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

9 views (last 30 days)
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?

Answers (3)

Torsten
Torsten on 23 Feb 2022
... or
intersect(i1,intersect(i2,i3))

David Hill
David Hill on 23 Feb 2022
f=find(ind1==ind2&ind2==ind3&ind1==ind3);
  2 Comments
Shayma Al Ali
Shayma Al Ali on 23 Feb 2022
Thank you for your response! I tried it but I recieved an error saying the arrays have incompatible sizes.
David Hill
David Hill on 23 Feb 2022
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);

Sign in to comment.


Bruno Luong
Bruno Luong on 23 Feb 2022
i1 = [1 3 4];
i2 = [4 4 2 5];
i3 = [5 4 1 5 3 3 2];
mintersect(i1, i2, i3)
  1 Comment
Bruno Luong
Bruno Luong on 24 Feb 2022
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

Sign in to comment.

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!