How i will choose a random element of a matrix

51 vues (au cours des 30 derniers jours)
Giannakis Stoukas
Giannakis Stoukas le 24 Mar 2015
Commenté : Walter Roberson le 21 Mai 2022
I have a matrix A,and i want to choose randomly an element from the matrix.How i will do that?

Réponse acceptée

per isakson
per isakson le 24 Mar 2015
One way
A = rand(3,4)
A( randi([1,size(A,1)],1), randi([1,size(A,2)],1) )
  2 commentaires
Walter Roberson
Walter Roberson le 21 Mai 2022
L(randi(numel(L), npop, n))

Connectez-vous pour commenter.

Plus de réponses (1)

Guillaume
Guillaume le 24 Mar 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.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by