matlab - reducing matrices under certain conditions -Part 3- -


m matrix composed of several submatrix ai such ai(1:3,j) same vector 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 initial data problem m , v.

my goal is eliminate sub-matrix ai of m, if ai(4,:) not contains @ least numel(v)-2 values of v.

for example a1, a2 , a3 verify condition. expected output (order of columns 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 change following code solve 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))) 

see if works -

%// id columns of m based on uniquenes of first thre rows [~,~,idx] = unique(m(1:3,:).','rows')  %//'  %// each id detect if satisfies ">= numel(v)-2" criteria  matches = accumarray(idx(:),m(4,:)',[],@(x) sum(ismember(v,x))>=numel(v)-2 )  %// use valid id's select valid unique groups desired output out = m(:,ismember(idx,find(matches))) 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -