Matlab在2个矩阵中找到最相似的行 [英] matlab find most similar rows in 2 matrices
问题描述
我有2个矩阵
A = [66 1 29.2;
80 0 29.4;
80 0 29.4;
79 1 25.6];
B = [66 1 28.2;
79 0 28.4;
66 1 27.6;
80 0 22.4]
我想找到匹配行的索引.
I would like to find the indeces of the matching rows.
indx = [1 1;
2 4;
3 2;
4 3]
idx表示A的row1与B的row1,A的row2与B的row4等匹配. 它应该成对匹配(A的1行与B的仅1行) 对于第2列中的值,应严格匹配.对于第1列和第3列的值,它应该是最匹配的.(即,如果存在一对具有相同值的对,则为好,否则我们应该选择最接近的值).
idx means that row1 of A matches with row1 of B, row2 of A with row4 of B etc. It should be a pairwise matching (1 row of A with only 1 row of B) For the values in column 2 it should be a strict match. For the values of columns 1 and 3 it should be the best match..(i.e. if it exist a pair with the same values good, otherwise we should pick the closest).
你能帮我吗? Tnx
Can you help me? Tnx
有关安德鲁评论的问题的更多见解
A的第3行无法匹配第4 B行,因为第4 B行已经与A的第2行匹配.A的第2行与B的第4行匹配,因为前两个元素80,0匹配,然后存在一个小错误在最后一个元素中(29.4-22.4 = 7). 可以说,正确匹配A和B的第二列比匹配第一列比匹配第三列更重要. 我
row 3 of A cant match row 4 B because row 4 B was already matched with row 2 of A. Row 2 of A matches row 4 of B because the first two elements 80,0 match and then there is a small error in the last element (29.4-22.4=7). We can say that matching properly the 2nd column of A and B is more important than matching the 1st column that is more important than matching the 3rd column. I
推荐答案
解决方案
感谢提供的评论,我设法提出了一个不优雅"但可行的解决方案.
Thanks to the comments provided I managed to come up with a "not elegant" but working solution.
B_rem = B;
weights_error = [2 4 1];
match = zeros(size(A,1),2);
for i = 1 : size(A,1)
score = zeros(size(B_rem,1),1);
for j =1 : size(B_rem,1)
score(j) = sum(abs(A(i,:) - B_rem(j,:)).*weights_error);
end
[~,idxmin] = min(score);
match(i,:) = [i,idxmin];
B_rem(idxmin,:)=[1000 1000 1000];
end
indx = match;
table_match = zeros(size(A,1),7);
table_match(:,1:3) = A(match(:,1),:);
table_match(:,5:7) = B(match(:,2),:);
这篇关于Matlab在2个矩阵中找到最相似的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!