Gray
Gray

Reputation: 35

MATLAB, extracting and saving values in a vector between 0 and 5, then 5 and 10, and so on

I have a column vector of values between 0 and 180. I'm looking for a way to extract all the values as columns between 0 and 5, then all the values between 5 and 10 and so on up to 180, and then save all these into a cell array.

I'm aware that I can use this sort of technique:

range = data(5 <= data & data <= 10) 

but it seems a little long winded to write this out for each range

Upvotes: 0

Views: 30

Answers (1)

Setsu
Setsu

Reputation: 1228

The straightforward approach is to wrap your code into a for loop, something like this:

maxValue = 180;
data = randperm(maxValue);   %//some dummy data
binSize = 5;
numBins = maxValue / binSize;
ranges = cell(1, numBins);

for i = 1:numBins 
    ranges{i} = data(binSize  * (i - 1) <= data & data <= binSize  * i);
end

Upvotes: 1

Related Questions