Averaging data

Hello,
I'm having difficult time trying to average my data. I have data which look like this
time1 = [0.14 0.99 1.67 2.45 3.10 3.70 4.24 5.00]
res1 = [4 6 2 9 1 0 4 7]
time2 = [0.11 0.69 1.00 1.45 1.66 2.04 2.24 2.99 3.11 3.43 4.25
4.55 5.00]
res2 = [8 1 5 3 7 1 3 10 9 5 3 2 1]
time3 = [0.09 0.33 1.13 1.44 2.10 2.70 3.24 4.00 4.80 5.00]
res3 = [0 3 4 7 2 6 3 4 9 8]
The period of time is always 5 min but there is different number of responses given during this time. These responses showing only a change at particular time and the value between time intervals is constant. I'd like to plot average of the data using a stairs function. Any help highly appreciated.

 Accepted Answer

Oleg Komarov
Oleg Komarov on 9 Aug 2011
time1 = [0.14 0.99 1.67 2.45 3.10 3.70 4.24 5.00];
res1 = [4 6 2 9 1 0 4 7];
axis([0 5 0 10])
hold on
% Plot stairs
stairs(time1,res1)
% Plot average
avg = sum(diff([0 time1])/time1(end) .* res1);
line([0,time1(end)],[avg avg],'Color','r')
One clarification, is the first element of res1 - 4 - valid from 0 to 0.14 or as it is it's ok?
EDIT
% unique time vector
untime = unique([time1 time2 time3]);
% Idx
[trash,idx1] = histc(untime,[0 time1(1:end-1) inf]);
[trash,idx2] = histc(untime,[0 time2(1:end-1) inf]);
[trash,idx3] = histc(untime,[0 time3(1:end-1) inf]);
rep = [res1(idx1); res2(idx2); res3(idx3)];
avg = mean(rep);
hold on
% Plot all the lines
stairs(untime,rep.')
% Plot the average in black
stairs([0 untime], [9 avg],'Color','k','Linew',2)

8 Comments

Marcel
Marcel on 9 Aug 2011
Thanks, but stairs function is not a problem here.
I'd like to plot an average of all data, not only time1 vs.res1.
Oleg Komarov
Oleg Komarov on 9 Aug 2011
Can you be more specific? I added a line which is the average of res1, is it still not what you want?
Marcel
Marcel on 9 Aug 2011
ok, I'll try to describe it more in details.
As I wrote above my data is not only time1 and res1. I have three responses from three diffrent subjects(res1, res2, res3) within 5min time intervals(time1, time2, time3). I'd like to get an average of these three responses. The responses are some sort of scores which are given at specific time (for example 4 is given at 0.14min, 6 at 0.99min - between 0.14 and 0.99min the score is 4 and the change to 6 happens at 0.99). In other words, if you plot these three sets of data (time1, res1; time2, res2; time3, res3) in the same figure, then what I'm looking for is the average of these three plots.
Hope it's more clear now.
Marcel
Marcel on 9 Aug 2011
I forgot to put 0min in the time vectors, at the start(0 min) the value is always 9.
Oleg Komarov
Oleg Komarov on 9 Aug 2011
See my edit.
Fangjun Jiang
Fangjun Jiang on 9 Aug 2011
Brilliant, Oleg! I was thinking about interp1() but it doesn't allow duplicated x values. I assume untime is something like untime=0:0.01:5. You might want to add that to avoid another question from the OP.
Also, consider adding data to make res=9 when time=0 (indicated by the OP) for all three series and modify your histc() lines too.
Fangjun Jiang
Fangjun Jiang on 9 Aug 2011
or untime=unique([time1,time2,time3]) should work and is better.
Oleg Komarov
Oleg Komarov on 9 Aug 2011
forgot to post it...it was exactly that!

Sign in to comment.

More Answers (1)

Marcel
Marcel on 10 Aug 2011

0 votes

Thanks very much guys. This is exactly what I was looking for. I realy appreciate your help.

4 Comments

Marcel
Marcel on 12 Aug 2011
I have one more question. How to average the data within .5 min time intervals. I mean, to get one average point for every .5min across all responses. Having these average points I'd like to plot it using stairs function with error bars for each point.
Thanks in advance
Oleg Komarov
Oleg Komarov on 12 Aug 2011
Then create:
untime = 0:0.5:5
instead of untime = unique(...);
Marcel
Marcel on 12 Aug 2011
Thanks Oleg, you are very helpfull.
What is the best option to plot error bars in my case? I was trying to use errorbar function but did not work very well for me...
Oleg Komarov
Oleg Komarov on 12 Aug 2011
What behaviour did you observe with error bar that didn't satisfy you?

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!