MATLAB Answers

Obtaining min value each month across several years

조회 수: 26(최근 30일)
Si Raven
Si Raven 12 Jan 2021
댓글: Si Raven 12 Jan 2021
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

  댓글 수: 0

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 12 Jan 2021
편집: Matt J 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

Si Raven
Si Raven 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 :)

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by