Info

This question is closed. Reopen it to edit or answer.

How to aggregate data and calculate its standard deviation

1 view (last 30 days)
Hi
I have two matrix with 1) day 2) travel time (sec). Where there are several observations of travel times per day. And date for more than two years.
day .......Traveltime
1 .............220
1 .............230
1 .............240
2 .............210
2 .............210
...etc
How can I aggregate the data_, so that I will have only one observation (average travel time) per day, and the standard deviation for each day.
So that the output, will be something like this:
day.......average_time.......Std_dev
1............230.............10
2............210.............0
I would appreciate if you can help me.
  2 Comments
per isakson
per isakson on 8 Jul 2013
Edited: per isakson on 8 Jul 2013
Firstly, a couple of questions:
  • The input data is that a text file? Or double array? Or a cell array?
  • " are several observations of travel times per day" does that mean that the length of the lines/row varies?

Answers (1)

dpb
dpb on 8 Jul 2013
Edited: dpb on 8 Jul 2013
I'll presume you have two vectors d and t; if so you can make a matrix as
MATl
>> m = [d t]
m =
1 220
1 230
1 240
2 210
2 210
>> [u,~,c] = unique(m(:,1)); % get the unique days, locations
>> [u,accumarray(c,m(:,2),[],@mean),accumarray(c,m(:,2),[],@std)]
ans =
1 230 10
2 210 0
>>
Or, of course, you can use the two vectors directly w/o the concatenations...
MATL
>> [u,~,c] = unique(d);
>> [u,accumarray(c,t,[],@mean),accumarray(c,t,[],@std)]
ans =
1 230 10
2 210 0
>>
Why didn't just use them first time is beyond me... :)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!