Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.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_Vector_Set Union - Fatal编程技术网

Matlab 如何对由另一个向量表示的矩阵行进行并集?

Matlab 如何对由另一个向量表示的矩阵行进行并集?,matlab,matrix,vector,set-union,Matlab,Matrix,Vector,Set Union,我想对矩阵的一些行进行并集x。必须进行并集的行的行号由向量r给出。MATLAB中有没有内置函数可以实现这一点 x = [1 2 4 0 0; 3 6 5 0 0; 7 8 10 12 9; 2 4 6 7 0; 3 4 5 8 12]; r = [1, 3, 5]; 你可以这样做 >>> union(union(x(r(1),:),x(r(2),:)),x(r(3),:)) ans = 0

我想对矩阵的一些行进行并集
x
。必须进行并集的行的行号由向量
r
给出。MATLAB中有没有内置函数可以实现这一点

x = [1  2  4  0  0; 
     3  6  5  0  0;
     7  8 10 12  9;
     2  4  6  7  0;
     3  4  5  8 12];

r = [1, 3, 5]; 

你可以这样做

>>> union(union(x(r(1),:),x(r(2),:)),x(r(3),:))

ans =

  0     1     2     3     4     5     7     8     9    10    12

或者为设置一个循环,循环遍历向量
r
,以计算所有的并集

您可以这样做

>>> union(union(x(r(1),:),x(r(2),:)),x(r(3),:))

ans =

  0     1     2     3     4     5     7     8     9    10    12

或者为设置一个循环,该循环迭代向量
r
,以计算所有的并集

我认为这对您很有用-首先,使用所需的行获取子矩阵
x(r,:)
,然后找到其中所有唯一的值:

unique(x(r,:))

ans =
     0
     1
     2
     3
     4
     5
     7
     8
     9
    10
    12

我认为这对您很有用-首先,使用子矩阵
x(r,:)
和您想要的行,然后找到其中所有唯一的值:

unique(x(r,:))

ans =
     0
     1
     2
     3
     4
     5
     7
     8
     9
    10
    12