Effacer les filtres
Effacer les filtres

how to make a matrix elements randomly distributed

1 vue (au cours des 30 derniers jours)
Pham Ngoc Thanh
Pham Ngoc Thanh le 21 Juin 2013
how to make a matrix m elements, which has n elements "one" or #0, randomly distributed, the remaining elements are zero (n<m).

Réponse acceptée

David Sanchez
David Sanchez le 21 Juin 2013
Try this out (adapt the values to your needs):
m = 10;
n = 3;
pos = randperm(m*n,m);
M = zeros(m,n);
M(pos) = 1;
M =
0 0 0
1 1 0
0 1 1
0 0 0
1 0 0
0 0 0
0 0 0
1 0 1
0 1 1
0 0 1
M has the ones randomly distributed.
  1 commentaire
Jan
Jan le 21 Juin 2013
And for older Matlab versions:
M = zeros(m,n);
pos = randperm(m*n);
M(pos(1:n)) = 1;

Connectez-vous pour commenter.

Plus de réponses (1)

Pham Ngoc Thanh
Pham Ngoc Thanh le 21 Juin 2013
thanks a lot.

Catégories

En savoir plus sur Matrices and Arrays 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