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

5 次查看(过去 30 天)
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