Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
matlab中的矩阵求交?_Matlab_Matrix - Fatal编程技术网

matlab中的矩阵求交?

matlab中的矩阵求交?,matlab,matrix,Matlab,Matrix,这几乎和这个问题一样 区别在于:如果所有矩阵的元素(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;

这几乎和这个问题一样

区别在于:如果所有矩阵的元素(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];
/更新:必须修复代码,现在应该是正确的。

我会这样做

A = A1+A2+A3;
B = (A1==A2)&(A1==A3);
C = (A1==0)+(A2==0)+(A3==0);

D = ones(3)*-1;
D(B==1) = A1(B==1);
D(C==2) = A(C==2);
  • B
    记录所有矩阵中编号相同的元素的位置
  • C
    记录两个矩阵为0的元素的位置

然后,我们可以使用矩阵
B
C
中的信息修改
D
的元素,其值最初设置为-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

代码

%%// 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)
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;
这将产生与上一版本相同的输出

第3版

假设-如果在A1、A2和A3的相应位置的三个元素中,只有两个相同,则取与其他两个不同的元素作为最终矩阵B

代码

%%// 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)
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;

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

我建议不要再次发布该问题,尤其是在相互发布的几天内。只需修改你的另一个问题,让它更清楚地说明你在问什么。@MZimmerman6:我不这么认为。修改已经回答的问题可能会让任何稍后阅读的人感到非常困惑。如果A1和A2中只有两个对应的元素相同,而A3中则不同,该怎么办。我们是不是在取这三个元素的
max
?在我看来,从你的
B
@Daniel来看,我认为这是高度依赖于具体情况的,但我觉得对上一个问题的修改不会引起很大的混乱,只要它表明事情发生了变化。