Create matrix using the array as index
Show older comments
Hi,
I have an array of 1*2000 double. I want to create matrix of 6*2000 double. so only the index of the value is 1 and the others is zero in each columns.
for example if my array is a=[2 2 1 0 3]
the ouput of of matrix will be same as the image below?

Thank you.
Accepted Answer
More Answers (1)
Try this
M = zeros(5,5);
a = [2,2,1,0,3];
for jj = 1:5
M(a(jj)+1,jj)=1;
end
disp(M)
Categories
Find more on Matrix Indexing 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!