Reputation: 59
I'm trying to write a program which extracts the minimum temperatures from a data-set, data
, for a given month, time and year.
Meaning that a person should be able to select a time, start-/endyear and get a matrix, lowTempsOverYears
, which should contain all the lowest-recored temperatures for january-december between two selected years at certain time.
Demonstrating what I mean I'll give a brief example. Take the two years: 1997-2001 and a time say 1200. This should give me a matrix containg the lowest tempartures recored for all months between the years 1997 and 2001. The output should be a 4x12 matrix where I have 4 different temperatures for every column which denotes the month.
You can find my program below:
function algo= getMiniserie(data, startYear, endYear, time)
YearInterval = startYear:1:endYear;
for month = 1:12
lowTempsOverYears = zeros(length(YearInterval),12);
for yearNumber = 1:length(YearInterval)
year = YearInterval(yearNumber);
p = extractperiod(data,year,month,time);
if ~isempty(p)
q = min(p);
lowTempsOverYears(yearNumber,month) = q;
end
end
algo = lowTempsOverYears;
end
end
The data
variable, from which I extract my data, consists of 3 columns and 400k+ so rows.
*first column denotes the date(YYYYMMDD)
*second column denotes the time
*third column denotes the temperature
And what the extractperiod
function does is that it, as the name would suggest, extracts all temperatures for a given month/year/time.
When I try to call my function by:
>> getMiniserie(data, 1997, 2001, 1200)
I get https://i.sstatic.net/D88gy.jpg .
Any ideas to how I could improve my code to get my desired output?
My idea was that to make a variable which stores all the minimum values for each iteration of month.
So I initilized lowTempsOverYears
to make it a(in this particular case wherethe start-/endyear is 1997 and 2001) 4x12
matrix. Where during the first month-iteration it stores all the minimum temperatures for january in the first column , where all the selected years are represented by the rows.
Please feel free to ask if I've omitted something from my explanation, I'll happily add to the picture.
code for extractperiod
function mdata = extractperiod(data,year,month,time)
x = year*100 + month;
k = find(floor(data(:,1)/100) == x & (data(:,2) == time));
mdata = data(k,3);
end
Upvotes: 1
Views: 29
Reputation: 323
Because the first command inside your month loop is lowTempsOverYears = zeros(length(YearInterval),12);
, you're resetting lowTempsOverYears
to a matrix of zeros each time through the loop. This erases the output of each previous loop. The final time through the loop, you reset all values to zero, then fill in the 12th column.
Move the line lowTempsOverYears = zeros(length(YearInterval),12);
outside your month loop, as shown below.
function algo= getMiniserie(data, startYear, endYear, time)
YearInterval = startYear:1:endYear;
lowTempsOverYears = zeros(length(YearInterval),12);
for month = 1:12
for yearNumber = 1:length(YearInterval)
year = YearInterval(yearNumber);
p = extractperiod(data,year,month,time);
if ~isempty(p)
q = min(p);
lowTempsOverYears(yearNumber,month) = q;
end
end
algo = lowTempsOverYears;
end
end
Upvotes: 1