Thursday 15 March 2012

matlab - Array filter based on multiple columns -



matlab - Array filter based on multiple columns -

suppose have 4 x n array:

a = [1 2 3 4; ... 2 4 8 9; ... 6 7 9 4; ... 1 8 3 4];

i want filter whole array based on content of first 2 columns.

for example, if want homecoming array rows contain 2 in first 2 columns, reply i'm looking isl

r = [1 2 3 4;... 2 4 8 9];

or, if want homecoming rows containing 1 in first 2 columns, reply i'm looking is...

a = [1 2 3 4;... 1 8 3 4];

i'm sure it's obvious how can in matlab? filtering whole array based on find or evaluation commands (e.g. a == 2) totally fine. it's filtering based on multiple columns in order can't figure out.

to check a given number, apply any along 2nd dimension restricted desired columns, , utilize logical index select desired rows:

cols = [1 2]; %// columns @ val = 1; %// value r = a(any(a(:, cols)==val, 2), :);

if want several values, example, select rows contain either 2 or 3 in columns 1 or 2: utilize ismember instead of ==:

cols = [1 2]; %// columns @ vals = [2 3]; %// values r = a(any(ismember(a(:, cols), vals), 2), :);

if want check if numbers within range:

cols = [1 2]; %// columns @ v1 = 6; %// numbers should greater or equal this... v2 = 8; %// ...and less r = a(any(a(:, cols)>=v1, 2) & any(a(:, cols)<v2, 2), :);

arrays matlab matrix

No comments:

Post a Comment