MATLAB: Easy? How to remove data below or above a certain value

abovebelowMATLABremovevaluevector

I have a long vector, with values between 0 and 10,000. I'd like to separate this into three new vectors: one containing only the values between 0 and 1,000; one containing only the values between 1,000 and 5,000; and one containing the values above 5,000. How can I do this?

Best Answer

First of all, it's not long - far, far from it. To find values in a range, you need to create a logical vector that says whether an element is, or is not, in the range. Like this:
vector = 9000*rand(1, 100); % Sample data
% Extract elements in the first range.
inRange = vector < 1000;
firstRange = vector(inRange);
% Extract elements in the second range.
inRange = vector > 1000 & vector <= 5000;
secondRange = vector(inRange);
% Extract elements in the third range.
inRange = vector > 5000;
thirdRange = vector(inRange);