简体   繁体   English

Matlab,如何将行矩阵的每个元素与另一个行矩阵的每个元素进行比较?

[英]Matlab, How do I compare each element of a row matrix with each element of another row matrix?

I have two matrices in Matlab: 我在Matlab中有两个矩阵:

q = [3 4 5];  
w = [5 6 7];

I want to compare every element of q with w (ie 3 compared with 5, 6, and 7). 我想比较q每个元素与w (即3与5,6和7比较)。 If it matches any element in w (like how 5 is in both q and w ) then both q and w share 5 as a common key. 如果它在任何元素相匹配w (例如如何5是在两个qw ),然后两者qw作为公共密钥份5。

How can I compute all the common keys for q and w ? 如何计算qw所有公共密钥?

Try 尝试

>> x = intersect(q,w)

x = 

    5

This function treats the input vectors as sets and returns the set intersection. 此函数将输入向量视为集合并返回集合交集。 I think this is what you wanted to know. 我想这就是你想知道的。 Is there a match yes/no? 有匹配是/否? if x is empty (numel(x)==0) there was no match. 如果x为空(numel(x)== 0)则没有匹配。

q = [3 4 5];
w = [5 6 7];

%# @sellibitze
intersect(q,w)

%# @Loren
q( ismember(q,w) )

%# Me :)
q( any(bsxfun(@eq, q, w'),1) )
[Q W] = meshgrid(q, w)
% Q =
%      3     4     5
%      3     4     5
%      3     4     5
% W =
%      5     5     5
%      6     6     6
%      7     7     7
Q == W
% ans =
%      0     0     1
%      0     0     0
%      0     0     0

如果您需要有关匹配的更多信息,请查看ismember,特别是第二和第三输出论据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM