I am trying to randomly disperse different numbers in MATLAB array:

10 visualizaciones (últimos 30 días)
I am trying to randomly disperse different numbers in MATLAB array:
I have two 3's, four 2's and I want to randomly populate ones vector (size 10,1).
End Result look something like this:
A = [1; 3; 1; 2; 3; 2; 2; 1; 1; 2; 1; 1]
Then I want to fix the values in A but add more random elements but I can only replace with higher numbers:
for example, to the matrix above I will randomly add two more 2's and two more 3's giving something like this
A2 =[ 3; 3; 2; 2; 3; 2; 2; 2; 1; 2; 1; 3]
  2 comentarios
Jos (10584)
Jos (10584) el 25 de Feb. de 2014
Editada: Jos (10584) el 25 de Feb. de 2014
Two questions:
  1. Is it also allowed to replace 2's but 3's, or can only 1's change into higher number?
  2. Do you specify how many new 2's or 3's have to appear, or do you only set the total number of changes?
Keith
Keith el 25 de Feb. de 2014
Jos,
only 1's and 2's can change. 3 is the max number. I will fix the total number of changes myself.
Thanks

Iniciar sesión para comentar.

Respuesta aceptada

Jos (10584)
Jos (10584) el 25 de Feb. de 2014
Editada: Jos (10584) el 25 de Feb. de 2014
V = ones(1,10)
V(1:4) = 2
V(5:6) = 3
V = V(randperm(numel(V))) % randomize the order initially
% change four 1's into two 2's and two 3's
idx = find(V==1) % where are the 1's
idx = idx(randperm(numel(idx)))
V(idx(1:4)) = [2 2 3 3]

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by