Wednesday, June 18, 2008

advanced use of reshape

on this posting i explained how to use reshape. Now i want to share with you a good idea i had today.

The idea is to use reshape to acces some part of a vector. Lets say, A is a vector with 35040 entries (365 days, 24 hours, 4 values per hour... 4 values per hour one year long). If you want to make a sum of A for every day, you can do that with a for-loop. this isn't very fast. Instead of that, you can use reshape:

B=reshape(A,24*4,[]);

B is a 96 (one day) x 365 Matrix. Then, you can just make sum(B) and you have the sum of all days into a vector without using any loop!

No comments: