Assignment and accessing using indices
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jeff Wood
am 5 Jun. 2016
Kommentiert: Jeff Wood
am 6 Jun. 2016
I had a question about how to do a particular type of assignment using indices. The code below is a simplified example.
k = zeros(4,4);
v = [2,3];
k(v,v) = 1;
My intention was to get a result that looks like
ans =
[0, 0, 0, 0;
0, 1, 0, 0;
0, 0, 1, 0;
0, 0, 0, 0]
But unfortunately the result is:
ans =
[0, 0, 0, 0;
0, 1, 1, 0;
0, 1, 1, 0;
0, 0, 0, 0]
So my question is: Is there a way to access individual elements (rather than rectangular regions) without using a for loop (Which I understand is computationally expensive in MatLab)?
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (2)
Chad Greene
am 5 Jun. 2016
Try this:
A = zeros(4);
lin = sub2ind([4 4],2:3,2:3);
A(lin) = 1;
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical 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!