how can i convert the 1 sec file to 1 min by averageing the reading of each 60 rows and the program repeat it every 60 rows?

i had two data series of magnetic survey, one every 1 sec and the other every 1 min. how can i convert the 1 sec file to 1 min by averageing the reading of each 60 rows and the program repeat it every 60 rows?

 Accepted Answer

The Matlab "trick" for this is to rely on memory storage order...
x=mean(reshape(x,60,[])).';
leaves column vector of the averages of the 60-element columns created by the reshape operation. If you number of elements isn't precisely divisible by 60, you'll need to account for it in the second argument when reshape to the multiple or truncate first.

5 Comments

please, can you clarify the function of that code x=mean(reshape(x,60,[])).'; as i cant submit it . i think also that i can write a function that make the program sum every 60 rows then divide by 60, but i don't know the code if u know please.
this is a sample of the data file
-24.72 165.6 152.85 0 226.71030170683
-24.69 165.58 152.84 1 226.68568128578
-24.73 165.56 152.83 2 226.6686908243
-24.61 165.63 152.88 3 226.74047587495
-24.72 165.58 152.83 4 226.68220860932
-24.59 165.64 152.87 5 226.73886874552
-24.65 165.6 152.83 6 226.68919559608
-24.63 165.6 152.84 7 226.69376369896
the forth column is second counter and the file is a registration for one day, so i want to write a program to calculate the average of each 60 rows for all the columns
Again, use the memory storage order of Matlab and the vectorized functions. The first code assumed just a vector but the idea works for arrays as well. For your above data observe the above to average over 2 rows can be written as
>> Navg=2;
>> reshape(mean(reshape(x,Navg,[])),length(x)/Navg,[])
ans =
-24.7050 165.5900 152.8450 0.5000 226.6980
-24.6700 165.5950 152.8550 2.5000 226.7046
-24.6550 165.6100 152.8500 4.5000 226.7105
-24.6400 165.6000 152.8350 6.5000 226.6915
>>
To see this actually works, compare the results above to
>> [mean(x(1:2,:)); mean(x(3:4,:))]
ans =
-24.7050 165.5900 152.8450 0.5000 226.6980
-24.6700 165.5950 152.8550 2.5000 226.7046
for the first two results rows.
To average over 60 instead, set Navg=60;
To understand how it works, take each step for a small array like the above at a time at the command line and observe what occurs when
a) reshape to number of columns over which to average,
a=reshape(x,Navg,[])
b) take mean over that rearranged array,
b=mean(a)
c) and finally reshape to same number of columns as original
c=reshape(b,length(x)/Navg,[])
ADDENDUM
The best way (if there were one) to use a loop solution should be apparent from the sample above where compute the first two rows for comparison to the fully vectorized solution. That is, at least use the builtin mean function with the row indices computed over the loop rather than performing the base sums. Again, this is not recommended Matlab coding practice unless it is for tutorial purposes.
thanks alot i will try this, but could you tell me if for loop will work? and how?
It would, but why not use the facilities of Matlab array operations? It's the whole point of Matlab.

Sign in to comment.

More Answers (0)

Asked:

on 23 Aug 2014

Edited:

dpb
on 24 Aug 2014

Community Treasure Hunt

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

Start Hunting!