how can i fill matrix using a vector?
3 views (last 30 days)
Show older comments
Mira le
on 14 Nov 2019
Answered: JESUS DAVID ARIZA ROYETH
on 14 Nov 2019
my vector v1=[1 4 8 9 3 6 ]
my matrix pop with size N*m
N=10:
m=4;
I want to fill randomly matrix pop with v1 and 0 like this:
pop = 1 4 0 6
3 0 0 8
1 6 3 0
.
.please help me
0 Comments
Accepted Answer
JESUS DAVID ARIZA ROYETH
on 14 Nov 2019
solution:
v1=[1 4 8 9 3 6 ];
v2=[v1 zeros(1,4)];% change the amount of 0 to increase the probability in this case there are 4
pop=v2(randi(numel(v2),10,4))
More Answers (1)
JESUS DAVID ARIZA ROYETH
on 14 Nov 2019
without repetition :
v1=[1 4 8 9 3 6 ];
v2=[v1 zeros(1,4)];% change the amount of 0 to increase the probability in this case there are 4
[~, ind] = sort(rand(10,length(v2)),2);
pop=v2(ind(:,1:4))
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!