필터 지우기
필터 지우기

Indexing a matrix with another matrix?

조회 수: 2 (최근 30일)
Petter Stefansson
Petter Stefansson 2016년 9월 23일
댓글: Petter Stefansson 2016년 9월 23일
Hi.
I have a 10000x4 matrix, Idx, where each row contains 4 indices to a logical matrix, A. I would like to set all indices in Idx to true (1) in A. This seems like something that should be possible to do very easy in one single line but I can’t quite figure it out so I’m currently doing it by looping:
A = false(10000,256);
for i = 1 : size(A,1)
A(i,Idx(i,:)) = true;
end
which is very slow. Could someone tell me how to do this indexing without a loop?
Thanks

채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 9월 23일
편집: Andrei Bobrov 2016년 9월 23일
[m,n] = size(Idx);
[ii,~] = ndgrid(1:m,1:n); % or ii = repmat((1:m)',1,n);
% ii = (1:m)'*ones(1,n);
A(sub2ind(size(A),ii,Idx)) = true;
  댓글 수: 1
Petter Stefansson
Petter Stefansson 2016년 9월 23일
Thank you that was significantly faster

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by