Is there a way to do the following without running a for loop.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Srinivas Gopal Krishna
am 30 Jul. 2020
Kommentiert: Srinivas Gopal Krishna
am 31 Jul. 2020
clear all
y=randi(5,1,5)';
A=zeros(5,5);
for i=1:5,
A(i,y(i))=1;
end
I want to know if this can be implemented without using a for loop.
0 Kommentare
Akzeptierte Antwort
Bruno Luong
am 30 Jul. 2020
Bearbeitet: Bruno Luong
am 30 Jul. 2020
y = randi(5,1,5)'
Then
A = accumarray([(1:5)' y(:)], 1, [5 5]);
or
A = zeros(5,5);
A(sub2ind(size(A),1:5,y'))=1
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements 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!