我有三个1-d数组,其中元素是一些值,我想将一个数组中的每个元素与其他两个元素中的所有元素进行比较.
例如:
a=[2,4,6,8,12]
b=[1,3,5,9,10]
c=[3,5,8,11,15]
Run Code Online (Sandbox Code Playgroud)
我想知道不同数组中是否有相同的值(在这种情况下有3,5,8)
AB给出的答案是正确的,但是当您有3个阵列进行比较时,它是特定的.还有另一种替代方案可以轻松扩展到任意数量的任意大小的数组.唯一的假设是每个单独的数组包含唯一的(即非重复的)值:
>> allValues = sort([a(:); b(:); c(:)]); %# Collect all of the arrays
>> repeatedValues = allValues(diff(allValues) == 0) %# Find repeated values
repeatedValues =
3
5
8
Run Code Online (Sandbox Code Playgroud)
如果数组包含重复值,则在使用上述解决方案之前,需要在每个数组上调用UNIQUE.