登录论坛

查看完整版本 : MATLAB:比较三个数组中的所有元素


poster
2019-12-10, 20:30
我有三个1-d数组,其中的元素是一些值,我想将一个数组中的每个元素与其他两个元素中的所有元素进行比较。

例如:

a=[2,4,6,8,12] b=[1,3,5,9,10] c=[3,5,8,11,15] 我想知道不同数组中是否有相同的值(在这种情况下,有3、5、8)



回答:

AB给出 (https://stackoverflow.com/questions/2425066/matlabcomparing-all-elements-in-three-arrays/2425911#2425911)的答案 (https://stackoverflow.com/questions/2425066/matlabcomparing-all-elements-in-three-arrays/2425911#2425911)是正确的,但是它特定于您要比较的3个数组的情况。还有另一种选择,可以轻松扩展到任意大小的任意数量的数组。唯一的假设是每个单独的数组都包含唯一(即未重复)的值:

>> allValues = sort([a(:); b(:); c(:)]); %# Collect all of the arrays >> repeatedValues = allValues(diff(allValues) == 0) %# Find repeated values repeatedValues = 3 5 8 如果数组包含重复的值,则在使用上述解决方案之前,您需要在每个数组上调用UNIQUE (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/unique.html) 。



更多&回答... (https://stackoverflow.com/questions/2425066)