how can i fill matrix using a vector?

조회 수: 2 (최근 30일)
Mira le
Mira le 2019년 11월 14일
답변: JESUS DAVID ARIZA ROYETH 2019년 11월 14일
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

채택된 답변

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019년 11월 14일
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))
  댓글 수: 1
Mira le
Mira le 2019년 11월 14일
It works, thank you, but there is a duplicate values in pop, how do we remove it an replace it by 0?
Example:
pop = 6 0 1 1 4 become 6 0 1 0 4

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019년 11월 14일
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))

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by