How to transform a vector into a binary matrix with row constraint ?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!