在某些条件下减少矩阵 - 第3部分

时间:2015-04-24 18:21:05

标签: matlab matrix

M是一个由多个子矩阵Ai组成的矩阵,Ai(1:3,j)j = 1,...,size(Ai,2)的相同向量

M = [1022  3001  4451 1022 1022  3001 3001 1022 4451 1022;
      112    45    10  112  112    45   45  112   10   11;
      500    11    55  500  500    11   11  500   55   88;
        2     6     6    5   71     2   71   88    2    2]



A1 = [1022 1022 1022 1022;
       112  112  112  112;
       500  500  500  500;
         2    5   71   88]

A2 = [3001 3001 3001;
        45   45   45;
        11   11   11;
         6    2   71]

A3 = [4451 4451;
        10   10;
        55   55;
         6    2]

A4 = [1022;
        11;
        88;
         2]

V = [2 71 6 9]

此问题的唯一初始数据是MV

我的目标,如果Ai的{​​{1}}值不包含M,则Ai(4,:)的所有子矩阵numel(V)-2将被删除{1}}。

我的示例VA1A2验证了这一情况。 预期的输出(列的顺序并不重要):

A3

如何更改以下code以解决我的问题:

[1022 1022 1022 1022 3001 3001 3001 4451 4451 ;
  112  112  112  112   45   45   45   10   10;
  500  500  500  500   11   11   11   55   55;
    2    5   71   88    6    2   71    6    2]

1 个答案:

答案 0 :(得分:1)

看看这是否适合你 -

%// ID columns of M based on the uniquenes of the first thre rows
[~,~,idx] = unique(M(1:3,:).','rows')  %//'

%// For each ID detect if it satisfies the ">= numel(V)-2" criteria 
matches = accumarray(idx(:),M(4,:)',[],@(x) sum(ismember(V,x))>=numel(V)-2 )

%// Use the valid ID's to select the valid unique groups for desired output
out = M(:,ismember(idx,find(matches)))
相关问题