필터 지우기
필터 지우기

How do I find if the three arrays of unequal size have the same values?

조회 수: 4 (최근 30일)
Shayma Al Ali
Shayma Al Ali 2022년 2월 23일
댓글: Bruno Luong 2022년 2월 24일
I have three arrays (ind1, ind2, ind3) that contain indices for three other arrays (ph1,ph2,ph3). ph1 ph2 and ph3 are functions of time/distance that are occuring at the same time but at different distances. Ind1 Ind2 and Ind3 contain the indices of when the values of ph are valid for the respective array. I would like to know when the arrays (ind1,ind2,ind3) have the same values so I can know when ph1 ph2 and ph3 have valid values at the exact same time. My current code is:
%check if the same points in each track are the same
lia=ismember(ind1,ind2);
lia1=find(lia==1);
lib=ismember(ind3,lia1);
lib1=find(lib==1);
Is there a better way to do this?

답변 (3개)

Torsten
Torsten 2022년 2월 23일
... or
intersect(i1,intersect(i2,i3))

David Hill
David Hill 2022년 2월 23일
f=find(ind1==ind2&ind2==ind3&ind1==ind3);
  댓글 수: 2
Shayma Al Ali
Shayma Al Ali 2022년 2월 23일
Thank you for your response! I tried it but I recieved an error saying the arrays have incompatible sizes.
David Hill
David Hill 2022년 2월 23일
Maybe I misunderstood you. Did you not mean (ind1,ind2,ind3) have the same values on the same row? Or did you mean having the same values anywhere in all three arrays? If the latter, then:
temp=ind1(ismember(ind1,ind2));
temp=ind3(ismember(ind3,temp));%these are the numbers that are the same in all three arrays
If they need to be the same across the row, you could padd the shorter arrays with nan to make them the same size.
m=max(length(ind1),length(ind2),length(ind3));
ind1=[ind1,nan(1,m-length(ind1))];
ind2=[ind2,nan(1,m-length(ind2))];
ind3=[ind3,nan(1,m-length(ind3))];
f=find(ind1==ind2&ind2==ind3&ind1==ind3);

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


Bruno Luong
Bruno Luong 2022년 2월 23일
i1 = [1 3 4];
i2 = [4 4 2 5];
i3 = [5 4 1 5 3 3 2];
mintersect(i1, i2, i3)
  댓글 수: 1
Bruno Luong
Bruno Luong 2022년 2월 24일
mintersect can also return the (first) place where the commmon elements are located
i1 = [1 2 4];
i2 = [4 4 2 5];
i3 = [5 4 1 5 3 3 2];
[common l1 l2 l3] = mintersect(i1, i2, i3);
common
i1(l1)
i2(l2)
i3(l3)
result
common =
2 4
ans =
2 4
ans =
2 4
ans =
2 4

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by