Reputation: 563
M
is a matrix composed of several submatrix Ai
such that Ai(1:3,j)
is the same vector for 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]
The only initial data for this problem are M
and V
.
my goal is to eliminate all sub-matrix Ai
of M
, if Ai(4,:)
does not contains at least numel(V)-2
values of V
.
for my example A1
, A2
and A3
verify this condition.
The expected output (order of columns is not important):
[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]
how to change the following code to solve my problem:
[~,~,idx] = unique(M(1:3,:)','rows') %//'
valid = ismember(M(4,:),V)
valid_idx = accumarray(idx(valid),M(4,valid).',[],@(x) ...
numel(unique(x)))>=numel(V) %//'
out = M(:,ismember(idx,find(valid_idx)))
Upvotes: 1
Views: 52
Reputation: 221624
See if this works for you -
%// 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)))
Upvotes: 1