I have a huge amount of data with time stamps concerning temperature and displacement of a bridge deck.
I want to look at hourly intervals and have therefore assigned all the time stamps to their corresponding hour starting from a specific point in time, namely 1/1-2013 00:00.
This means that my data vectors now look like this:
if true (this is the only way I could find to make the following in vector form) end Time[h] Temperature[deg C] Displacement[cm] 0 5.45 0.133 0 5.57 0.136 0 5.68 0.140 1 6.02 0.145 1 6.24 0.147 2 6.56 0.149 2 6.78 0.152 2 6.76 0.152 2 6.80 0.153 ... .... .....
Since I want to plot the relationship between temperature and displacement with hourly time steps, I would like to compute the mean value of every hour. This should go into a new vector containing only the hourly averaged time stamps.
My problem is that every hour has a different amount of data points, e.g. hour 0 has three, hour 1 has two and hour 2 has four. How do I go about this without making loops? I imagine that a vectorized method would save a lot of running time compared to loops.
The result I want should look something like the following:
if true (this is the only way I could find to make the following in vector form) end Time[h] Temperature[deg C] Displacement[cm] 0 5.57 0.136 1 6.13 0.146 2 6.73 0.152 ... .... .....
I hope that I made the problem clear, otherwise I would be happy to elaborate.
Thanks in advance.
No products are associated with this question.
This is a perfect job for accumarray:
avgTemperature = accumarray(Time + 1, Temperature, , @mean);
Unfortunately I have severe problems to understand the documentation of this command. Because I do not have access to Matlab currently, I can only guess, if the shown method is working.
[EDITED] Although accumarray allows a very compact solution, here a simple FOR loop:
Time1 = Time + 1; % No zeros as indices n = length(Time1); maxTime = max(Time1); accum = zeros(1, maxTime); len = zeros(1, maxTime); for k = 1:n index = Time1(k); accum(index) = accum(index) + Temperature(k); len(index) = len(index) + 1; end result = accum ./ len;
(Not tested, typos might exist)
I remember this was answered before, maybe a year ago or so. I don't remember anything to search about it. On the other hand, this is fairly easy with the Image Processing Toolbox. The time is your "labels" and you just call regionprops() on each column to get the values of each label (time). Then sum up and divide by the number in each label. Do you have the Image Processing Toolbox?