![]() |
MATLAB:比较三个数组中的所有元素
我有三个1-d数组,其中的元素是一些值,我想将一个数组中的每个元素与其他两个元素中的所有元素进行比较。
例如: a=[2,4,6,8,12] b=[1,3,5,9,10] c=[3,5,8,11,15] 我想知道不同数组中是否有相同的值(在这种情况下,有3、5、8) [B]回答:[/B] [URL="https://stackoverflow.com/questions/2425066/matlabcomparing-all-elements-in-three-arrays/2425911#2425911"]AB给出[/URL]的[URL="https://stackoverflow.com/questions/2425066/matlabcomparing-all-elements-in-three-arrays/2425911#2425911"]答案[/URL]是正确的,但是它特定于您要比较的[B]3个[/B]数组的情况。还有另一种选择,可以轻松扩展到任意大小的任意数量的数组。唯一的假设是每个单独的数组都包含唯一(即未重复)的值: >> allValues = sort([a(:); b(:); c(:)]); %# Collect all of the arrays >> repeatedValues = allValues(diff(allValues) == 0) %# Find repeated values repeatedValues = 3 5 8 如果数组包含重复的值,则在使用上述解决方案之前,您需要在每个数组上调用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/unique.html"]UNIQUE[/URL] 。 [url=https://stackoverflow.com/questions/2425066]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 05:03。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.