Create matrix using the array as index
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/761461/image.jpeg)
Thank you.
0 Kommentare
Akzeptierte Antwort
Rik
am 8 Okt. 2021
You don't even need a loop:
a=[2 2 1 0 3];
ind=a+1;
A=zeros(max(ind),numel(ind));
ind=sub2ind(size(A),ind,1:numel(ind));
A(ind)=1
0 Kommentare
Weitere Antworten (1)
per isakson
am 8 Okt. 2021
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)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!