To pick a number randomly
Mostrar comentarios más antiguos
Can some one help in picking a number from a matrix for example i have matrix of the form A=[1 2 3 4 5; 6 6 7 5 8 ; 12 4 5 6 9] and i require to pick only one element randomly from the matrix but should not be the same element every time.
Respuesta aceptada
Más respuestas (1)
The answer of andrei does not give a truly random number since he uses the unique command.
This will:
A=[1 2 3 4 5; 6 6 7 5 8 ; 12 4 5 6 9];
B = round((length(A(:))-1)*rand+1);
A(B)
ps. as far as rand returns a truly random number
2 comentarios
PK
el 12 de Sept. de 2012
Andrei Bobrov
el 12 de Sept. de 2012
Editada: Andrei Bobrov
el 12 de Sept. de 2012
A2 = reshape(A',[],1);
out = A2(randperm(numel(A),1));
Categorías
Más información sobre Language Fundamentals en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!