Using the min and max function for form a new variable
3 views (last 30 days)
Show older comments
Hello
I am calculating HDD (heating degree days) and CDD (cooling degree days)and have managed to reshape the data to get a dailyT =reshape(data:,4)24,365). Now I need to use the min and max functions to create new variables (1 x 365) of daily min and max temperature and I am stuck on this stage.
Please can you assist on how to do this.
Regards
Farai
0 Comments
Answers (3)
Adam
on 27 Oct 2014
Edited: Adam
on 27 Oct 2014
If your data is (24,365) then you can just use the normal form:
dailyMins = min( data );
dailyMaxes = max( data );
to get (1,365)-sized results.
If you wanted to be explicit you can use:
dailyMins = min( data, [], 1 );
to tell min to calculate along dimension 1, thus returning a result of size equal to dimension 2, but since this is the default behaviour that is un-necessary. However, if your data happened to be (365,24) then you could use this method with a '2' as the final argument to achieve the same result.
0 Comments
Farai Mwashita
on 28 Oct 2014
2 Comments
Adam
on 28 Oct 2014
That is more than a reshape option, you would need to break down your 365 days into months, average each of these and then end up with a length 12 result.
e.g.
m = [31 28 31 30 31 30 31 31 30 31 30 31];
to give you the lengths of each month. Then a generic version of the following example:
monthlyAverage(1) = mean( dailyMins( 1:m(1) ) )
monthlyAverage(2) = mean( dailyMins( (m(1) + 1):( m(1) + m(2) ) ) );
etc, etc.
There are neater ways to do that of course in a generalised way than just writing out 12 hard-coded lines, but I'll leave that as an exercise for you.
You may find
cumsum( m );
to be very useful in simplifying the indexing into m.
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!