Relative occurance of intersect values

조회 수: 1 (최근 30일)
Gabriel Stanley
Gabriel Stanley 2022년 9월 20일
댓글: Star Strider 2022년 9월 22일
Is there a way to vectorize the below, and/or a better way to go about it?
A = %some set of numbers
B = %another set of numbers
C = intersect(A,B);
for temp = 1:numel(C)
C(temp,2) = numel(find(A==C(temp,1)));
C(temp,3) = numel(find(B==C(temp,1)));
end
I've tried using histcounts(A,C), however that returned 1 fewer bins that required, and just adding an arbitrary additional bin edge to either side does not get histcounts to give me what I'm looking for.

채택된 답변

Star Strider
Star Strider 2022년 9월 20일
The intersect funciton can output the indices of the intersecting elements. See Intersection of Two Vectors and Their Indices for an illustration.
  댓글 수: 4
Gabriel Stanley
Gabriel Stanley 2022년 9월 22일
Well, I ended up rewriting my code in such a way that ismember has been very useful as the index map. Thanks!
A = [unique(A(:,1)), accumarray(findgroups(A(:,1)),A(:,2),[],@func)]
B = [unique(B(:,1)), accumarray(findgroups(B(:,1)),B(:,2),[],@func)]
C = intersect(A(:,1),B(:,1))
A2Bmap = ismember(A(:,1),C)
B2Amap = ismember(B(:,1),C)
%more code
Star Strider
Star Strider 2022년 9월 22일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by