MATLAB: Select all of an array EXCEPT in given ranges

CaptainProg picture CaptainProg · Jun 15, 2012 · Viewed 9.1k times · Source

I'd like to perform metrics on the contents of an array that do NOT fall within certain ranges.

For example, I have an array with 1000 rows and 2 columns. I'd like to perform a mean() calculation on all the elements in one column (let's say column #2) that don't fall in rows 50-150, 250-300, 400-700 and 900-950.

Thus, the mean should be calculated based on rows 1-49, 151-249, 301-399, 701-899 and 951-1000.

Any ideas how to go about this?

Edit: I should point out that those items which are included will change each time the program is run. Therefore, I can't just hard-code the inclusions in; they need to be worked out based on the exclusions.

Answer

Amro picture Amro · Jun 15, 2012

How about:

M = rand(1000,2);
idx = setdiff(1:size(M,1), [50:150, 250:300, 400:700, 900:950]);

MM = M(idx,:)

Now apply any function to the filtered matrix:

mean(MM,1)