Effacer les filtres
Effacer les filtres

Issue with using loop

2 vues (au cours des 30 derniers jours)
Suman Dhamala
Suman Dhamala le 19 Juin 2021
Commenté : Jan le 19 Juin 2021
I have 3 dimensional matrix, 135*129*43099. 43099 being the daily rainfall value for 118 years(1901 to 2018). I intend to convert this daily rainfall value to monthly rainfall value, resulting matrix 135*129*1416.
For this I created a matrix of size 1416*5. Each row representing each month for 118 years. First column is year, second column is month, third column is number of number of days in a month (irrevelant and not used here), fourth and fifth column is the index. Gilmpse of such matrix's small portion is below.
Name of matrix: 'statarrayD'
1901 1 31 1 31
1901 2 28 32 59
1901 3 31 60 90
1901 4 30 91 120
1901 5 31 121 151
1901 6 30 152 181
Here is the deal, I want to add 1st to 31st value of those 43099 values to place it into a first row, similarly for second row, i want to add 32nd to 59th value of 43099 values. This way i would have 1416 rows in the resulting matrix, formed as a result of adding those 43099 values, which is named MRain here.
Code:
MRain=NaN(135,129,1416);
x=1;
for i=1:1416
MRT=NaN(135,129,43099);
min=statarrayD(i,4);
max=statarrayD(i,5);
MRT(:,:,min:max)=temp(:,:,min:max);
MRain(:,:,x)=sum(MRT,3);
x=x+1;
end
MRT is the temporary matrix that would return monthly values. Say when value of i=1 then, it would return the 1st to 31st rainfall values, remaining being NaN. min and max would return 1 and 31 respectively. min and max are basically 4th and 5th column of the matrix shown above. I am using sum function to add the MRain montly values, to get one value for each month.
This code seems to work perfectly without loop, when i use the numerical value like 1, 2, 3 instead of value of i, like code shown below. But returns NaN value for entire MRain matrix with used with loop. I am missing something in the loop.
%test%
MRest=NaN(135,129,1416);
MRTest=NaN(135,129,35);
min=statarrayD(1,4);
max=statarrayD(1,5);
MRTest=temp(:,:,min:max);
MRest(:,:,1)=sum(MRTest,3);
  4 commentaires
dpb
dpb le 19 Juin 2021
Look into tables, timetables, datetime, grouping variables with findgroups and splitapply
There are tools built for these kinds of analyses that, with a datetime variable associated with thd data can make these things a cakewalk...
Jan
Jan le 19 Juin 2021
Hint: avoid using "min" and "max" as variables, because shadowing built-in functions cause unexpected behavior frequently, if you try to use the functions later:
clear all
max(1:10) % 10
max = 17;
max(1:10) % error

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by