create random matrix with 4 vectors
2 views (last 30 days)
Show older comments
Anna Fabry
on 18 Nov 2014
Answered: Azzi Abdelmalek
on 18 Nov 2014
How can I create a random matrix X that has n rows, and each row is [1 0 0], [0 1 0], [0 0 1] or [0 0 0]. On top of that, the first possibility has to appear x times, the second one y times and the third one again z times. In other words sum(X)= [x y z]?
0 Comments
Accepted Answer
Azzi Abdelmalek
on 18 Nov 2014
a=[1 0 0; 0 1 0;0 0 1]
x=5
y=4
z=7
n=x+y+z
b=zeros(n,3)
ii1=randperm(n,x)
b(ii1,:)=repmat(a(1,:),x,1)
ii=setdiff(1:n,ii1)
ii2=ii(randperm(numel(ii),y))
b(ii2,:)=repmat(a(2,:),y,1)
ii=setdiff(ii,ii2)
b(ii,:)=repmat(a(3,:),z,1)
0 Comments
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!