0

I'm new to Matlab. I need help with matrix comparison.

I have three matrices: R, S and T (size: 95956 x 1) and I need to compare all of their elements and see which elements are equal. Then we multiply the same values by a fourth matrix.

In a Matlab file, I read the coordinates and separate them into three matrices. Thus obtaining matrices R, S and T saved in "data.mat". Then I read in another file "data.mat" and used a For loop:

for t=1:1:length(CoordinateIndex)
    index = R == S;
    ts=ts+1;
end

Is this correct and how I will multiply only those values which are equal to a fourth matrix?

4

1 回答 1

0

使用一些简单的示例矩阵和逻辑索引,我们可以通过以下方式找到所有三个共同的值:

R = [1:4 1:4 1:4 1:4];
S = [1:2 1:2 1:4 1:6 1:2];
T = [1:6 1:2 1:8];
R_common = R(R==S&R==T);

这会产生:

R_common =

     1     2     1     2     3     4

如果您想要所有三个矩阵中共有的索引,请使用:

I = find(R==S&R==T);

这使

I = 
     1     2     9    10    11    12

编辑:正如@Adiel 建议的那样,您可能对矩阵中常见的值感兴趣,无论它们在矩阵中的位置如何。这可以通过intersect函数简单地完成(另见this):

RST_intersection = intersect(intersect(R,S),T)

对于我的示例矩阵,这给出了

RST_intersection =

     1     2     3     4

请注意, 的参数intersect可以是不同的长度,这可能会有所帮助。

于 2013-07-07T18:26:22.737 回答