Borrar filtros
Borrar filtros

How i will choose a random element of a matrix

19 visualizaciones (últimos 30 días)
Giannakis Stoukas
Giannakis Stoukas el 24 de Mzo. de 2015
Comentada: Walter Roberson el 21 de Mayo de 2022
I have a matrix A,and i want to choose randomly an element from the matrix.How i will do that?

Respuesta aceptada

per isakson
per isakson el 24 de Mzo. de 2015
One way
A = rand(3,4)
A( randi([1,size(A,1)],1), randi([1,size(A,2)],1) )
  2 comentarios
Assen Beshr
Assen Beshr el 21 de Mayo de 2022
Editada: Assen Beshr el 21 de Mayo de 2022
per isakson: perfect . I have one question related to this question. the question is How I choose random value npop X n matrix if npop =5 and n=2; from column matrix L=[2;4;5;7;8;9;12;25;32;33]
for example [4 25
2 7
33 12
32 5
4 9];
Walter Roberson
Walter Roberson el 21 de Mayo de 2022
L(randi(numel(L), npop, n))

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 24 de Mzo. de 2015
It may be (insignificantly) faster to use linear indexing:
A(randi(numel(A)));
It's more succinct anyway, and works whatever the number of dimensions of the array.

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