在Matlab中矩阵的交集? [英] Intersection of matrices in matlab?

查看:194
本文介绍了在Matlab中矩阵的交集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎与此问题相同矩阵交集

区别是:如果所有矩阵的元素(i,j)的交集都是相同的数字,则不输出-1,而是输出该数字.下面是一个示例:

A1 = [2, 2, 0;
      2, 2, 0;
      0, 2, 0];


A2 = [2, 0, 4;
      4, 3, 0;
      0, 0, 1];


A3 = [2, 0, 0;
      1, 0, 3;
      3, 4, 3];

我想获得以下矩阵:

 B = [2,  2,  4;
     -1, -1,  3;
      3, -1, -1];

解决方案

版本1

out1 = -1.*(A1~=A2).*(A1~=A3).*(A2~=A3)
max_mat = max(cat(3,A1,A2,A3),[],3)
out1(~out1) = max_mat(~out1)

输出

out1 =

     2     2     4
    -1    -1     3
     3    -1    -1

版本2:也许是更快的版本

假设-如果在A1,A2和A3中对应位置的三个元素中只有两个相同,则将这三个元素的最大值作为最终矩阵B. /p>

代码

%%// Concatenate all three A matrices
A=cat(3,A1,A2,A3,A1);

%%// Logical matrix with ones where all three elements are different from each other
out1 = -1.*all(diff(A,[],3)~=0,3)

%%// Get the max values, to be stored where -1 all three corresponding elements 
%%// are not different from each other
max_mat = max(A,[],3)

%%// Get the final output
out1(~out1) = max_mat(~out1)

这将产生与先前版本相同的输出.

版本3

假设-如果在A1,A2和A3的相应位置的三个元素中,只有两个相同,那么对于最终矩阵,取与其他两个元素不同的元素, B.

代码

A=cat(3,A1,A2,A3,A1);
AA = A(:,:,1:3);
t1 = bsxfun(@ne,AA,mode(AA,3));
out1 = max(AA.*t1,[],3) + all(~t1,3).*A1;
out1(all(diff(A,[],3)~=0,3))=-1;

这将产生与以前版本相同的输出.

It is almost the same question as this one Matrices intersection!

The difference is this: If the intersection of the element (i, j) of all matrices is the same number then do not output -1 but output this number. An example is the following:

A1 = [2, 2, 0;
      2, 2, 0;
      0, 2, 0];


A2 = [2, 0, 4;
      4, 3, 0;
      0, 0, 1];


A3 = [2, 0, 0;
      1, 0, 3;
      3, 4, 3];

I want to get the follow matrix:

 B = [2,  2,  4;
     -1, -1,  3;
      3, -1, -1];

解决方案

Version 1

out1 = -1.*(A1~=A2).*(A1~=A3).*(A2~=A3)
max_mat = max(cat(3,A1,A2,A3),[],3)
out1(~out1) = max_mat(~out1)

Output

out1 =

     2     2     4
    -1    -1     3
     3    -1    -1

Version 2: Maybe a faster version

Assumption - If out of the three elements in the corresponding positions across A1, A2 and A3, only two are same, then take the max of those three elements for the final matrix, B.

Code

%%// Concatenate all three A matrices
A=cat(3,A1,A2,A3,A1);

%%// Logical matrix with ones where all three elements are different from each other
out1 = -1.*all(diff(A,[],3)~=0,3)

%%// Get the max values, to be stored where -1 all three corresponding elements 
%%// are not different from each other
max_mat = max(A,[],3)

%%// Get the final output
out1(~out1) = max_mat(~out1)

This produces the same output as the previous version.

Version 3

Assumption - If out of the three elements in the corresponding positions across A1, A2 and A3, only two are same, then take the element that is different from the other two for the final matrix, B.

Code

A=cat(3,A1,A2,A3,A1);
AA = A(:,:,1:3);
t1 = bsxfun(@ne,AA,mode(AA,3));
out1 = max(AA.*t1,[],3) + all(~t1,3).*A1;
out1(all(diff(A,[],3)~=0,3))=-1;

This produces the same output as the previous versions.

这篇关于在Matlab中矩阵的交集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆