Selecting multiple array elements
조회 수: 23 (최근 30일)
이전 댓글 표시
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
채택된 답변
Fangjun Jiang
2022년 7월 5일
편집: Fangjun Jiang
2022년 7월 5일
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)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!