How can I generate a matrix with exact number of ones(with respect to random distribution) ?
1 view (last 30 days)
Show older comments
I need to generate a random matrix(large one) with the exact number of ones? thanks in advance :)
2 Comments
Roberto
on 18 May 2014
Please explain... What's the exact number of ones? and please give an example of the desired result!
Accepted Answer
Matt J
on 18 May 2014
Edited: Matt J
on 18 May 2014
A=zeros(N);
A(randperm(numel(A),nOnes))=1; %nOnes is desired number of ones
3 Comments
Jos (10584)
on 19 May 2014
Over 8 years ago I wrote a wrapper function for this cal http://www.mathworks.com/matlabcentral/fileexchange/10924-ntrue/content/ntrue.m ent/ntrue.m
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!