How to convert row to matrix with below format????
1 次查看(过去 30 天)
显示 更早的评论
I need to convert row to matrix. For example, A=[1 2 3 4 5]
Need answer like this, Ans=[1 0 0 0 0; 0 1 0 0 0; 0 0 1 0 0; 0 0 0 1 0; 0 0 0 0 1]
7 个评论
采纳的回答
更多回答(1 个)
Birdman
2018-6-6
编辑:Birdman
2018-6-6
Something like this should work:
A=[3 1 1 4];
Ans=zeros(max(size(A)));
r=1:max(size(A));
c=A;
idx=sub2ind(size(repmat(A,max(size(A)),1)),r,c);
Ans(idx)=1
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!