问题
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
is an entity to explain what I want.
A
contains unique column vectors of M(1:3,:)
, in addition to the corresponding values in M(4,:)
A(1:3,:) = unique(M(1:3,:)','rows')'
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;]
the column vector [5002;431;1100]
was eliminated because [2;10;55]
is contained in X{2} = [2 10 55 9 17]
the column vector [622;124;88]
was eliminated because [32 12] = X{4}
回答1:
Inputs:
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').';
gives:
>> A
A =
622 1007 4044 5002
124 552 300 431
88 2010 1113 1100
Then using unique
and accumarray
[~, ~, subs] = unique(M(1:3,:)','rows');
A4 = accumarray(subs(:),M(4,:).',[],@(x) {x});
Now we have A4
as cell-array
>> A4
A4 =
[2x1 double] [4x1 double] [2x1 double] [3x1 double]
Then using cellfun
, ismember
, all
and any
%// 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
Displaying the columns we want
out = A(:,idxC)
Results:
>> out
out =
1007 4044
552 300
2010 1113
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 :)
来源:https://stackoverflow.com/questions/30391956/matrix-manipulation-to-extract-certain-sub-columns