Reputation: 107
Using MATLAB (R2015b) I have a time-series A
that is a 4000x3 matrix. Below is a sample where the first column is serial-time (here its from 13:56:37 to 14:25:09) and the second and third columns are measurements:
1.0e+05 *
7.366965809837962 0.000082000000000 0.062000000000000
7.366965810300925 0.000082500000000 0.013000000000000
7.366965836689815 0.000083000000000 0.040000000000000
7.366965857291667 0.000085000000000 0.013000000000000
7.366965875810186 0.000086000000000 0.010000000000000
7.366965876851851 0.000086000000000 0.020000000000000
7.366965889351852 0.000087000000000 0.015000000000000
7.366965892245370 0.000087000000000 0.016000000000000
7.366965896875000 0.000087000000000 0.004000000000000
7.366965897337963 0.000087500000000 0.050000000000000
7.366965901967592 0.000087500000000 0.002000000000000
7.366965906018518 0.000093000000000 0.010000000000000
7.366965907060185 0.000093000000000 0.010000000000000
7.366965928587963 0.000092000000000 0.010000000000000
7.366965962268518 0.000091500000000 0.030000000000000
7.366965973263889 0.000092000000000 0.001000000000000
7.366965974768519 0.000092000000000 0.050000000000000
7.366965982060186 0.000090000000000 0.010000000000000
7.366965993402777 0.000090000000000 0.090000000000000
7.366966007986111 0.000090000000000 0.043330000000000
How can I calculate the average for column two and three per every five minutes (instead of per event)? So for the first 5 minutes 13:55:00 - 14:00:00 we should get the average value 8.225 for middle column and 3750 for the right column, i.e. something like:
13:55:00 8.225 3750
14:00:00 8.4 2650
I have tried without success so any help is much appreciated.
Upvotes: 0
Views: 360
Reputation: 1279
The nice thing about this problem is that you can break it into easy chunks. First of all, lets break the matrix in to vectors
times=A(1,:);
measurementX=A(2,:);
measurementY=A(3,:);
The second part is you need to break the time up into 5 minute increments. Lets make a vector of times starting at 7.366965809837962e5
(your first time) and going up to 7.366966007986111e5
(your last time) in 5 minute increments. Unfortunately, I cannot tell how many ticks exist in a 5 minute increment in MATLAB, I'll arbitrarily pretend it is 123e3. You will need to replace that number correctly.
timeIncrements=7.366965809837962e5:123e3:7.366966007986111e5
Now we need to find all indicies of times after the start of one increment and before the start of the next increment:
for (i=1:length(timeIncrements)-1)
indicies{i}=times>timeIncrements(i)&×<=timeIncrements(i+1);
end
Now we get the averages
for i=1:length(indicies)
measurementX_5minIncrements(i)=mean(measurementX(indicies{i}));
measurementY_5minIncrements(i)=mean(measurementY(indicies{i}));
end
Huge Note! This is untested but provides the general gist of what you need to do. I don't have access to MATLAB and can't play around to make sure it is correct. Also, this is very verbose, there is a two line answer to your problem but without actually using MATLAB I can't think it up (there was a time when I would have known it off the top of my head, but alas, that day is no more). Good luck!
Upvotes: 1
Reputation: 326
I think this could help you
%Inital reference position
pos=1;
%Counter
count=1;
for i=2:20
%Elapsed seconds since the reference position
elapsed_seconds=etime(datevec(a(i,1)),datevec(a(pos,1)));
if elapsed_seconds>5*60 %Calculate means if larger than 5 minutes
matrix(count,1)=a(pos,1);
matrix(count,2)=mean(a(pos:(i-1),2));
matrix(count,3)=mean(a(pos:(i-1),3));
count=count+1; %Update counter
pos=i; %Update reference position
end
end
matrix
Upvotes: 2