矩阵操作以提取某些子列 [英] Matrix manipulation to extract certain sub columns

查看:129
本文介绍了矩阵操作以提取某些子列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

M = [1007  1007  4044  1007  4044  1007  5002 5002 5002 622 622;
      552   552   300   552   300   552   431  431  431 124 124 ; 
     2010  2010  1113  2010  1113  2010  1100 1100 1100  88  88;
        7    12    25    15    12    30     2   10   55  32  12]

X = {[2 5 68 44],[2 10 55 9 17],[1 55 6 7 8 9],[32 12]}

A = [1007  4044  5002  622
      552   300   431  124
     2010  1113  1100   88
        7    25     2   32
       12    12    10   12
       15          55
       30                 ]

A是一个解释我想要什么的实体.

A is an entity to explain what I want.

AM(4,:)

A(1:3,:) = unique(M(1:3,:)','rows')'

我希望找到A(1:3,:)的列向量,其在M(4,:)中的对应值不属于单元格X的向量之一(显然不等于这些向量之一).

I hope to find the column vectors of A(1:3,:) whose the corresponding values in M(4,:) are not part of one of the vectors of the cell X (and obviously not equal to one of these vectors).

在我的示例中,期望的结果是矩阵:

for my example the desired result is the matrix:

 [1007  4044; 
   552   300; 
  2010  1113;]

列向量[5002;431;1100]被消除了,因为X{2} = [2 10 55 9 17]中包含了[2;10;55]

the column vector [5002;431;1100] was eliminated because [2;10;55] is contained in X{2} = [2 10 55 9 17]

列向量[622;124;88]被删除,因为[32 12] = X{4}

推荐答案

输入:

M = [1007  1007  4044  1007  4044  1007  5002 5002 5002 622 622;
      552   552   300   552   300   552   431  431  431 124 124; 
     2010  2010  1113  2010  1113  2010  1100 1100 1100  88  88;
        7    12    25    15    12    30     2   10   55  32  12];

X = {[2 5 68 44],[2 10 55 9 17],[1 55 6 7 8 9],[32 12]};

执行此操作(您所做的事情)

Doing this (what you have done)

A(1:3,:) = unique(M(1:3,:).','rows').';

给予:

>> A

A =

     622        1007        4044        5002
     124         552         300         431
      88        2010        1113        1100

然后使用uniqueaccumarray

[~, ~, subs] = unique(M(1:3,:)','rows');

A4 = accumarray(subs(:),M(4,:).',[],@(x) {x});

现在我们将A4作为单元格数组

Now we have A4 as cell-array

>> A4

A4 = 

[2x1 double]    [4x1 double]    [2x1 double]    [3x1 double]

然后使用cellfunismemberallany

%// getting a mask of which columns we want
idxC(length(A4)) = false;
for ii = 1:length(A4)
    idxC(ii) = ~any(cellfun(@(x) all(ismember(A4{ii},x)), X));
end

显示我们想要的列

out = A(:,idxC)

结果:

>> out

out =

    1007        4044
     552         300
    2010        1113

我恳请您像@Dan所建议的那样尝试一下.如果您被卡在某个地方,可以参考一下.如果您有任何澄清/修改,请告诉我:)

I kindly recommend you try it yourself as @Dan suggests. If you are stuck at somewhere, you could refer this. If you have any clarification/modification, let me know :)

这篇关于矩阵操作以提取某些子列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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