How to transform a vector into a binary matrix with row constraint ?

조회 수: 1 (최근 30일)
Safia
Safia 2022년 9월 6일
댓글: Safia 2022년 9월 6일
Hi!
i have this vector v=[1,4,3,2], I want to transform it into a binary matrix under the following constraint: each row can contain only one '1'.
this is the required result
V=[1 0 0 0
0 1 0 0
0 1 0 0
0 1 0 0
0 1 0 0
0 0 1 0
0 0 1 0
0 0 1 0
0 0 0 1
0 0 0 1]
can anyone help me?
  댓글 수: 2
dpb
dpb 2022년 9월 6일
How did you invent the required result from the input?
Safia
Safia 2022년 9월 6일
I haven't got it yet, I just want the result in this form.

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

채택된 답변

Dyuman Joshi
Dyuman Joshi 2022년 9월 6일
From the apparent relation between v and the required matrix -
v=[1,4,3,2];
y=zeros(sum(v),numel(v));
z=1;
for i=1:numel(v)
y(z:v(i)+z-1,i)=1;
z=z+v(i);
end
y
y = 10×4
1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by