필터 지우기
필터 지우기

Problem with Matrix indexing using vector

조회 수: 1 (최근 30일)
Sebastian Raabe
Sebastian Raabe 2019년 11월 20일
편집: Jan 2019년 11월 20일
Hello, I have a problem with indexing matrix elements. I use the following code to describe my problem:
a = blkdiag(ones(3),ones(3));
[r,c] = find(a);
b = zeros(size(a));
b(r,c) = 1;
I assume that the matrix a and the matrix b are equal, but they are not. I can solve this problem with a for-loop, but for a very large matrix this solution is too slow. Are there any other solutions? Thank yoou for your help!

채택된 답변

Jan
Jan 2019년 11월 20일
편집: Jan 2019년 11월 20일
This is a job for sub2ind:
a = blkdiag(ones(3),ones(3));
[r,c] = find(a);
b = zeros(size(a));
index = sub2ind(size(a), r, c);
b(index) = 1;
isequal(a, b) % TRUE
Maybe using logical indexing is more efficient than find():
mask = (a == 1);
b = zeros(size(a));
b(mask) = 1;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by