Monday, 15 June 2015

arrays - Quick way to obtain parts of matrix satisying a condition with Matlab -



arrays - Quick way to obtain parts of matrix satisying a condition with Matlab -

i need fast way fetch parts in matrix , stack them in new matrix. suppose had matrix this:

m=

1 45 4 1 91 15 2 34 89 3 89 9 3 96 99

now want e.g. elements in lastly 2 columns s.t. left column has values [1; 3], i.e. output should this:

45 4 91 15 89 9 96 99

to e.g. part corresponding 1's on first column,

m(m(:,1) == 1,2:end),

and utilize 'or' logical parts corresponding [1;3], in real case have long vector of integers... there vectorized way of doing this?

i think loop might quite fast too, parts of matrix of different sizes, , stacking them might difficult...

see if fast plenty -

int_arr = [1 3]; %// array of integers select rows m(any(bsxfun(@eq,m(:,1),int_arr),2),2:end)

or

int_arr = [1;3]; %// array of integers select rows m(any(bsxfun(@eq,m(:,1)',int_arr)),2:end)

arrays matlab matrix vectorization

No comments:

Post a Comment