Matlab中两个向量之间的重叠

时间:2013-08-21 14:52:05

标签: matlab

我有这样的表:

    x   y   Z   w
u1  0   0   2   2
u2  1   0   1   2
u3  3   0   0   3
u4  2   2   5   9
u5  0   3   1   4

我想计算两行之间的重叠。我的Matlab代码是:

for j = 1:4
    x = data(j,:);
    for i = j+1:5
        y = data(i,:);
        a = sum(max(x,y));
        b = sum(min(x,y));
        d = sqrt(b);
        over = d/a;
    end
end 

但我想显示结果中行之间的重叠?例如,overlap(1,2)表示第一行和第二行之间的重叠。等等。结果是:     重叠(1,2)=?     重叠(1,3)=?     重叠(1,4)=?     ...     重叠(2,3)=?     重叠(2,4)=?     ... 我需要对代码进行哪些更改?  我想要mtarix:

Overlap(1,1)    Overlap(1,2)    Overlap(1,3)    Overlap(1,4)    Overlap(1,5)
 Overlap(1,2)   Overlap(2,2)    Overlap(2,3)    Overlap(2,4)    Overlap(2,5)
 Overlap(1,3)   Overlap(2,3)    Overlap(3,3)    Overlap(3,4)    Overlap(3,5)
 Overlap(1,4)   Overlap(2,4)    Overlap(1,3)    Overlap(4,4)    Overlap(4,5)
 Overlap(1,5)   Overlap(2,5)    Overlap(3,5)    Overlap(4,5)    Overlap(5,5)

而不是放置重叠(x,y)值。

2 个答案:

答案 0 :(得分:1)

你可以尝试这个,它返回每个行组合中重叠元素数量的矩阵:

function [ overlap ] = overlap( mat )
[hei, ~] = size(mat);
overlap = zeros(hei);
for i = 1:hei
    for j = 1:hei
        overlap(i,j) = sum(mat(i,:) == mat(j,:));
    end 
end
end

编辑:

这是你的意思吗?

data = [0   0   2   2;
    1   0   1   2;
    3   0   0   3;
    2   2   5   9;
    0   3   1   4];
[hei,~] = size(data);
over = zeros(hei);
for j = 1:hei-1
    x = data(j,:);
    for i = j+1:hei
        y = data(i,:);
        a = sum(max(x,y));
        b = sum(min(x,y));
        d = sqrt(b);
        over(j,i) = d/a;
    end
end
over

答案 1 :(得分:0)

我不知道为什么EitanT把他的答案拿走了,这对我来说似乎是正确的。所以我现在将这个编辑版本留在这里,直到:

data= [0   0   2   2
       1   0   1   2
       3   0   0   3
       2   2   5   9
       0   3   1   4]


for j = 1:4
    x = data(j,:);
    for i = j+1:5
        y = data(i,:);
        a = sum(max(x,y));
        b = sum(min(x,y));
        d = sqrt(b);
        over(i-1,j) = d/a;
    end
end 

over =

   0.34641   0.00000   0.00000   0.00000
   0.17678   0.24744   0.00000   0.00000
   0.11111   0.11111   0.11769   0.00000
   0.19245   0.19245   0.15746   0.13925

您需要做的就是将每个重叠计算保存到over矩阵的其他元素