Sum for every n number within a single column vector

I have a Precipitation vector 35090x1, which represents measurements taken every 30 minutes (48 measurements a day) over the course of 2 years. I would like to calculate daily totals for these two years by summing each of the 48 measurements taken each day. How do I do this?

3 Comments

Your vector length does not divide evenly by 48, i.e., you have a fractional day. How would you like that treated?
Sorry, I stated it wrong.. it's actually 35088x1, and because one of the year's was a leap year there are 731 days logged in total (an additional 48 recordings).

Sign in to comment.

 Accepted Answer

Using sepblockfun (Download),
Daily = sepblockfun(Precipitation,[48,1],'sum');

3 Comments

Realized I had to convert table to array... sometimes it's the simplest things that will getcha in the end haha. Thanks for the help!
Your input 'X' does not appear to be a vector, as you said, but rather a table.
Clearly, from the error message, your vector is actually a table.
daily = sepblockfun(yourtable.yourprecipitationvariable, [48 1], 'sum');
should work. However, if your table also has a column for the date then there are more reliable ways of doing what you want.

Sign in to comment.

More Answers (1)

dpb
dpb on 25 Mar 2018
Edited: dpb on 25 Mar 2018
The inside-information way is just
sum(reshape(x,48,[]))
making use of internal storage order and that Matlab functions operate column-wise by default. This does mean you have to know such details about data storage.
The higher-level route to more legible coding is as pointed to in the comment above or similar utilizing the special time-based functionality builtin to later releases.

Categories

Asked:

on 25 Mar 2018

Commented:

on 25 Mar 2018

Community Treasure Hunt

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

Start Hunting!