bzak
bzak

Reputation: 563

reducing matrices under certain conditions

M is a matrix 4x2000000 composed of several submatrix Ai such that Ai(1:3,j) is the same vector for j = 1,...,size(Ai,2). and Ai(4,j) are values between 1 and 100.

V = [2 15 21 43]

my goal is to eliminate all sub-matrix Ai of M, if Ai(4,:) does not contain all the values of V.

The only initial data for this problem are M and V

Example:

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

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;
        10;
        55;
         3]

A4 = [1022;
        11;
        88;
         2]

A5 =[3001;
       99;
        1;
        5]

if V = [2 71]

The expected output (order of columns is not important):

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

Upvotes: 0

Views: 62

Answers (1)

Divakar
Divakar

Reputation: 221624

One approach -

[~,~,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: 3

Related Questions