How do I find if the three arrays of unequal size have the same values?
10 次查看(过去 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?
0 个评论
回答(3 个)
David Hill
2022-2-23
f=find(ind1==ind2&ind2==ind3&ind1==ind3);
2 个评论
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
2022-2-23
Use FEX mintersect
i1 = [1 3 4];
i2 = [4 4 2 5];
i3 = [5 4 1 5 3 3 2];
mintersect(i1, i2, i3)
1 个评论
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 Center 和 File Exchange 中查找有关 Argument Definitions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!