How to convert row to matrix with below format????
Show older comments
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 Comments
Stephen23
on 6 Jun 2018
@Yuvaraj V: you have really specified how to generate the outpour matrix from the input vector. What would you expect the output from this vector to be?:
A = [5,1,1,4]
Yuvaraj Venkataswamy
on 6 Jun 2018
Stephen23
on 6 Jun 2018
@Yuvaraj V: you are changing your specification. First you wrote a comment where you stated that my example was not possible because 5 is larger than numel(A). Now you have deleted that comment and decided that it is possible. Please clarify which is the correct specification.
Yuvaraj Venkataswamy
on 6 Jun 2018
Edited: Yuvaraj Venkataswamy
on 6 Jun 2018
Stephen23
on 6 Jun 2018
Yuvaraj Venkataswamy
on 6 Jun 2018
Accepted Answer
More Answers (1)
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
2 Comments
@Birdman: there is a syntax error on this line:
Ans=zeros(max(size(A));
Categories
Find more on Annotations 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!