Obtaining min value each month across several years

4 views (last 30 days)
Hi,
I am looking for some help/pointers in how to pull out certain data from a matrix.
My matrix is a note of minimum temperature across each day for a 10 year period.
The matrix is arranged in columns temperature / day / month / year, and starts from 1 July 2000 and runs to 30 June 2010.
I am trying to write instructions to pull out the minimum temperature for each month as well as its row index.
I started trying to just pull the min temp for each month (and worry about row index later) but im alreadty stuck. I am trying to write it using 'for loops' but am open to other methods! I was thinking a for loop would be easier for me to follow.
So far i have got:
load(filename)
len=length(filename)
totalmonth= len/12
mintemp=zeros(totalmonth,1) %i guess will need to put 2 on the end when i get as far as index part
for years= 1990:2000 %so work through the dates
for month=1:12 %then for each year work through each month
mintemp=min(filename(filename(:,3)==month, 1); %may need to build the year check into this and i'm not sure about the '1' returning the data i want in column 1 - i doubt it!
end
I did find a group example on the help forums that would work if i was just after the month but could not work out how to make it also wrap around years as well.
So as you can see i'm good and stuck and would appreciate any help.
Si

Accepted Answer

Matt J
Matt J on 12 Jan 2021
Edited: Matt J on 12 Jan 2021
G=findgroups(filename(:,3),filename(:,4));
[mintemp,rowIndex]=splitapply(@func,filename(:,1),(1:numel(G)).',G)
function [minT,row]=func(T,R)
[minT,idx]=min(T);
row=R(idx);
end
  1 Comment
Si Raven
Si Raven on 12 Jan 2021
Thanks for replying.
Couldnt get it to work so had a tinker with the code and now all good:
>> G=findgroups(Temps(:,4),Temps(:,3));
[Mintemp,rowIndex]=splitapply(@min,Temps(:,1),G);
G was sorting by month and then year and i wanted it the other way round so in date order (easy to switch). Does mean i now have two outputs - MinTemp and rowIndex and would have preferred as a joint output - but the code was falling down otherwise.
Wouldnt have got there without your help, cheers :)

Sign in to comment.

More Answers (0)

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!