필터 지우기
필터 지우기

How To: Multi-level subindexing

조회 수: 2 (최근 30일)
Gabriel Stanley
Gabriel Stanley 2023년 2월 7일
댓글: Gabriel Stanley 2023년 2월 7일
I'm tryig to achieve a proper 1-line ndex/sub-indexing operation, but have thus far failed to get the syntax right
ArrayA = sparse(100,100000);
ArrayA(randi(100*100000,[7000 1])) = (82-36).*rand(7000,1)+36;
[ArrayAFull(:,1),ArrayAFull(:,2),ArrayAFull(:,3)] = find(ArrayA);
ArrayB = sparse(100,100000);
ArrayB(randi(100*100000,[7000 1])) = (90-30).*rand(7000,1)+30;
[ArrayBFull(:,1),ArrayBFull(:,2),ArrayBFull(:,3)] = find(ArrayB);
[~,IdxA,IdxB] = intersect(ArrayAFull(:,[1,2]),ArrayBFull(:,[1,2]),'stable','rows');
t1 = ArrayBFull(ArrayBFull(IdxB,1)==1,:); %This is where things go sour
%The next two lines get me the actually desired matrix
t2 = ArrayBFull(IdxB,:);
t2 = t2(t2(:,1)==1,:);
isequal(t1,t2); %false
Is it possible to compress the t2 lines into a single line, or no?

채택된 답변

Walter Roberson
Walter Roberson 2023년 2월 7일
Yes, it is, but it is fairly ugly to do so. You can call upon subsref() manually, possibly having used substruct() to help build the appropriate structure.
It is usually a lot easier to use a helper function. For example,
SelectRowWhere = @(expression, where) expression(expression(:,1)==where,:);
t2 = SelectRowWhere(ArrayBFul(IdxB,:), 1)
  댓글 수: 3
Walter Roberson
Walter Roberson 2023년 2월 7일
The two line version would typically be faster, as calling an anonymous function is notably slower than calling a non-anonymous function.
Gabriel Stanley
Gabriel Stanley 2023년 2월 7일
Understood. Ty for the heads-up.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel Computing에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by