I have three vectors in a for loop.
A=[1 2 3 7];
B=[2 2 4 6];
C=[1 3 2 5];
D=[];
I want to use intersect so that I will avoid elements that are common for the vectors. Like:
I wonder if there is a function for this? I read about mintersect, but I want to find all elements that exist in more than one vector
for i=1:length(A)
if intersect(A,B) || intersect(A,C) || intersect(B,C)
D(i)=0;
end
else
D(i)=1;
end

댓글 수: 4

Are you trying to find elements that are common to all vectors? e.g. 2, in this case?
A=[1 2 3 7];
B=[2 2 4 6];
C=[1 3 2 5];
D=intersect(intersect(A,B),C)
There are probably other ways still.
Joel Schelander
Joel Schelander 2021년 4월 15일
No I want to find elements that are common to at least two vectors. In this case 1, 2, 3
DGM
DGM 2021년 4월 15일
편집: DGM 2021년 4월 15일
Something like this?
D=unique([intersect(A,B) intersect(A,C) intersect(B,C)])
and I guess if you wanted to mask off elements in a given vector that were in the set D, you could do that
Dmask=~ismember(A,D)
The show code is not clear.
for i=1:length(A)
if intersect(A,B) || intersect(A,C) || intersect(B,C)
D(i)=0;
end
else % There is no IF for this ELSE ???
D(i)=1;
end
Neither the description in the original question, nor the code matchs the explanation given in the commen: "find elements that are common to at least two vectors"
Please case for asking clear question. The more confusing the question is, the more time you and the answering persons waste with not matching suggestions. You are not a beginner, but the code is for your master thesis.

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

 채택된 답변

Jan
Jan 2021년 4월 15일
편집: Jan 2021년 4월 15일

1 개 추천

If this is the goal: "find elements that are common to at least two vectors":
A = [1 2 3 7];
B = [2 2 4 6];
C = [1 3 2 5];
V = unique([A, B, C]);
D = V((ismember(V, A) + ismember(V, B) + ismember(V, C)) >= 2)
D = 1×3
1 2 3

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2017b

질문:

2021년 4월 15일

편집:

Jan
2021년 4월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by