比较矩阵中的值与公差范围

时间:2015-03-24 19:46:53

标签: matlab matrix comparison

我有4个矩阵,它们具有不同点的X,Y,Z坐标。这些矩阵是:

A = [1 2 5 5; 2 3 4 4; 44 5 65 55]

B = [1 3 4 5; 2 3 14 146; 4 5 45 1]

C = [2 4 5 6; 4 5 6 8; 3 44 5 66]

D = [4 5 6 8; 1 3 4 5; 12 3 4 5]

我想检查四个矩阵中是否有任何共同点。对于X,Y,Z,我的公差为+/- 1。

我可以为此运行一个循环但是会出现复杂性问题: 当矩阵数量增加时,代码的复杂性大大增加。对此有何出路?

关于这个问题的任何更好的程序?

2 个答案:

答案 0 :(得分:0)

您的问题有点不清楚,但我假设您正在寻找指数i,j,其中A(i,j)== B(i,j)== C(i,j)== D(我,j),在一些容忍范围内?如果是这样,这段代码就可以了。

A=[ 1 2 5 5; 2 3 4 4; 44 5 65 55];
B=[ 1 3 4 5; 2 3 14 146; 4 5 45 1];
C=[ 2 4 5 6 ; 4 5 6 8; 3 44 5 66];
D=[4 5 6 8; 1 3 4 5; 12 3 4 5];

% concatenate A,B,C,D into a Nrow x Ncol x Nvars matrix
M = cat(3, A,B,C,D);

% find maximum and minimum along dimension 3
maxVal = max(M, [], 3);
minVal = min(M, [], 3);

% find which row,col indices have a range within 0.00001
valRange = maxVal - minVal;
matchPoints = (valRange <= 0.00001);

% boolean value representing if any match was found
matchFound = any(matchPoints(:));

答案 1 :(得分:0)

感谢您的回复。我写了这个函数来比较两个矩阵:

function [comp] = compare( A, B )

[m,n]= size(A);
[mm,nn]=size(B);
k=1;
comp=[];
for ii=1: m
for i=1:mm
    if A(ii,1) < (B(i,1)+2) & A(ii,1)> (B(i,1)-2)
        if A(ii,2) < (B(i,2)+2) & A(ii,2)> (B(i,2)-2)
            if A(ii,3) < (B(i,3)+2) & A(ii,3)> (B(i,3)-2) 
                comp(k,:)= [A(ii,:), B(i,:)];
                k=k+1;
            else continue
            end
        else continue
        end
    else continue
    end
end
end

end

我的数据是几天内激活的大脑坐标的输出。因此,我希望对群集中彼此相邻的区域(跨天)进行分组。我试图附上一张照片来解释我想要的东西。但堆栈溢出不允许我。

我希望这会进一步澄清我的帖子。