Selecting multiple array elements
23 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
reincornator
am 5 Jul. 2022
Kommentiert: reincornator
am 5 Jul. 2022
Hi.
n = 10;
A = rand(n);
B = A;
k = [1 1; 2 3; 4 5; 1 8; 8 6];
A(k(1,1),k(1,2))= 1;
A(k(2,1),k(2,2))= 1;
A(k(3,1),k(3,2))= 1;
A(k(4,1),k(4,2))= 1;
A(k(5,1),k(5,2))= 1;
B(k(:,1),k(:,2))=1;
isequal(A,B)
How can I assign a value of 1 to elements from array B with coordinates k using a single command?
0 Kommentare
Akzeptierte Antwort
Fangjun Jiang
am 5 Jul. 2022
Bearbeitet: Fangjun Jiang
am 5 Jul. 2022
n = 10;
A = zeros(n);
B = A;
k = [1 1; 2 3; 4 5; 1 8; 8 6];
A(k(1,1),k(1,2))= 1;
A(k(2,1),k(2,2))= 1;
A(k(3,1),k(3,2))= 1;
A(k(4,1),k(4,2))= 1;
A(k(5,1),k(5,2))= 1
B(sub2ind(size(A),k(:,1),k(:,2)))=1
isequal(A,B)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Multidimensional 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!