randomizing location of N zeros in matrix of ones
Afficher commentaires plus anciens
I need to make a matrix of ones (say 20X20).
Inside that matrix I need zero to appear randomly (in random locations) N times.
Réponse acceptée
Plus de réponses (1)
Jan
le 7 Fév 2011
EDITED: First version completely deleted after Walter found out, that Matt's algorithm works ~30% faster.
For larger matrices RANDPERM is slow. You can use the C-mex Shuffle from the FEX instead: http://www.mathworks.com/matlabcentral/fileexchange/27076
N = 7;
M = 20;
A = ones(M);
A(Shuffle(numel(A), 'index', N)) = 0;
For M=20, N=7 this is 4 times faster, for M=200, N=70 this is even 30 times faster than the method using RANDPERM.
1 commentaire
Tali
le 7 Fév 2011
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!