user3536870
user3536870

Reputation: 249

MATLAB collecting time data

I have the following data, 'd'

0.0012
0.0043
0.0034
0.0108
0.0042
0.0052
0.0516
0.0034
0.0023
.... and so on

I know this time data ranges from 0-0.1. What is the best way to group this un-ordered data into time ranges. I would want to collect the values in the range 0<=d<0.002, 0.002<=d<0.004 and so on in the 0.002 blocks until I reach 0.1.

Upvotes: 0

Views: 42

Answers (2)

Adriaan
Adriaan

Reputation: 18197

range = 0:0.002:0.1;
results = cell(numel(range)-1,1);
for ii = 1:numel(range)-1
    tmp = d>range(ii) & d<range(ii+1);
    results{ii} = d(tmp);
end

I initialised a range array, containing your range specifiers. tmp then contains logical indices on the location within your range, which get extracted and stored in the next line. Note that I used cells, since it is not guaranteed that each range has the same amount of points in it.

Upvotes: 2

Luis Mendo
Luis Mendo

Reputation: 112769

You can use accumarray:

step = .002;
limit = .1;
result = accumarray(1+floor(d(:)/step), d(:), [1+floor(limit/step), 1], @(x) {x});

In your example, this gives

>> result{1}
ans =
    0.0012
>> result{2}
ans =
    0.0034
    0.0034
    0.0023

etc.

Upvotes: 4

Related Questions