how to grab the last value in a loop?

Hi there, I would like to know how to get the last value of my loop. I am intersecting a matrix of 35 years of data with a matrix of 420 (420 months, from jan-1985 to dez-2019), where (B) is the intersect position. I am getting the mean value of the sine each 12 month, but in the last value (month 420) the result is NaN. How they are month, it has 31, 30, 29 and 28 days. So I also try to do and If statment, but it didn´t work. Some body could help me.
With the first example I got from 1 to 419 the nanmean values, but the 420 value don´t.
Thak you in advance.
%% first example
seno_m = NaN(length(Date_wd_monthly),1);
for i = 1:420
seno_m(i,1)= nanmean(sin(Date_wd_daily(B(i):B(i)-1,9)));
end
%% Example of the If statment
seno_m = NaN(length(Date_wd_monthly),1);
for i = 1:420
if numel(Date_wd_daily(B(i):B(i+1)-1,9))==31
seno_m(i,1)= nanmean(sin(Date_wd_daily(B(i):B(i,1)+31,9)));
elseif numel(Date_wd_daily(B(i):B(i+1)-1,9))== 30
seno_m(i,1)= nanmean(sin(Date_wd_daily(B(i):B(i,1)+30,9)));
elseif numel(Date_wd_daily(B(i):B(i+1)-1,9))== 29
seno_m(i,1)= nanmean(sin(Date_wd_daily(B(i):B(i,1)+29,9)));
else numel(Date_wd_daily(B(i):B(i+1)-1,9))== 28
seno_m(i,1)= nanmean(sin(Date_wd_daily(B(i):B(i,1)+28,9)));
end
end

 Accepted Answer

In your first code:
seno_m(i,1)= nanmean(sin(Date_wd_daily(B(i):B(i)-1,9)));
% ^^^^ ^^^^^^
This is the empty matrix in all cases. B(i):B(i)-1 does not contain any elements.
Teh 2nd code can be simplified:
seno_m = NaN(length(Date_wd_monthly),1);
for i = 1:420
n = numel(Date_wd_daily(B(i):B(i+1)-1,9));
seno_m(i,1)= nanmean(sin(Date_wd_daily(B(i):B(i,1) + n, 9)));
end
This can be expressed easier also, because numel(Date_wd_daily(B(i):B(i+1)-1,9)) is the same as:
numel(B(i):B(i+1)-1)
which is the same as:
B(i+1) - B(i);
If this equals 31, remember that B(i):B(i,1)+31 contains 32 elements (not 31).
To understand, what your code does and what it should do, the readers need to know what B contains.

6 Comments

Hello Jan, the letter (B) means the intersect position. I have a matrix of 35 years (daily values from 1-12783). Them I did an artificial montlhy date to get montlhy avergae values. After that I intersect the datenum value to get the average. But if I do what you recomended (n) changed. The thing is that the first examnple intersect from 1 to 419 run ok, and the result in the position 420 is NaN
seno_m = NaN(length(Date_wd_monthly),1);
for i = 1:420
n = numel(Date_wd_daily(B(i):B(i+1)-1,9));
seno_m(i,1)= nanmean(sin(Date_wd_daily(B(i):B(i,1) + n, 9)));
end % (n) change because each month has differente days ^
To get the 420 values I have to do this:
seno_m = NaN(length(Date_wd_monthly),1);
for i = 1:length(Date_wd_monthly)
seno_m(i,1)= nanmean(sin(Date_wd_daily(B(i,1):B(i+1)-1,9)));
end
The above example is right from 1 to 419, them position 420 in NaN matlab says "Index exceeds array bounds".
To get the last position, I have to do the next, to complement:
seno_m(420,1)= nanmean(sin(Date_wd_daily(12753:12783,9)));
Jan
Jan on 12 Apr 2021
Edited: Jan on 12 Apr 2021
"the letter (B) means the intersect position" - this does not clarify, what B contains. Something like this would be more clear:
B = [1, 32, 29, 32, ...]
I guess, that B contains the indices of the start points of each month. Then maybe this helps:
n = length(Date_wd_monthly);
seno_m = NaN(n, 1);
B(n+1) = numel(Date_wd_daily) + 1; % Additional point BEHIND the data
for i = 1:n
seno_m(i)= nanmean(sin(Date_wd_daily(B(i):B(i+1)-1, 9)));
end
Hi Jan, thank you for help me, but it still didn´t grab the last value. Any way I found another solution and it´s worked right. This is the code:
k=1
for i=1985:2019 %Years Looping
for j=1:12 %Months Looping
idx=find((Date_wd_daily(:,2)==i) & (Date_wd_daily(:,3)==j)); %Find all indeces for the specific month
meandata1= nanmean(sin(Date_wd_daily(idx,9)));
seno_m(k,:)=[i,j,meandata1]; %Store in a new matrix the mean values per month
k=k+1;
end
end
Now I would like to make a matrix (years (35), month (12)) with the mean values. I already try different loops, but I can´t. Do you know how can I group the values in a matrix. I already did it, but I would like to do in a loop.
QD_month = NaN (35,12);
QD_month (1,1:12) = seno_m (1:12,2);
QD_month (2,1:12) = seno_m (13:24,2);
QD_month (3,1:12) = seno_m (25:36,2);
QD_month (4,1:12) = seno_m (37:48,2);
QD_month (5,1:12) = seno_m (49:60,2);
QD_month (6,1:12) = seno_m (61:72,2);
QD_month (7,1:12) = seno_m (73:84,2);
QD_month (8,1:12) = seno_m (85:96,2);
QD_month (9,1:12) = seno_m (97:108,2);
QD_month (10,1:12) = seno_m (109:120,2);
QD_month (11,1:12) = seno_m (121:132,2);
QD_month (12,1:12) = seno_m (133:144,2);
QD_month (13,1:12) = seno_m (145:156,2);
QD_month (14,1:12) = seno_m (157:168,2);
QD_month (15,1:12) = seno_m (169:180,2);
QD_month (16,1:12) = seno_m (181:192,2);
QD_month (17,1:12) = seno_m (193:204,2);
QD_month (18,1:12) = seno_m (205:216,2);
QD_month (19,1:12) = seno_m (217:228,2);
QD_month (20,1:12) = seno_m (229:240,2);
QD_month (21,1:12) = seno_m (241:252,2);
QD_month (22,1:12) = seno_m (253:264,2);
QD_month (23,1:12) = seno_m (265:276,2);
QD_month (24,1:12) = seno_m (277:288,2);
QD_month (25,1:12) = seno_m (289:300,2);
QD_month (26,1:12) = seno_m (301:312,2);
QD_month (27,1:12) = seno_m (313:324,2);
QD_month (28,1:12) = seno_m (325:336,2);
QD_month (29,1:12) = seno_m (337:348,2);
QD_month (30,1:12) = seno_m (349:360,2);
QD_month (31,1:12) = seno_m (361:372,2);
QD_month (32,1:12) = seno_m (373:384,2);
QD_month (33,1:12) = seno_m (385:396,2);
QD_month (34,1:12) = seno_m (397:408,2);
QD_month (35,1:12) = seno_m (409:420,2);
You can omit the find() for a faster logical indexing:
idx = ((Date_wd_daily(:,2) == i) & (Date_wd_daily(:,3) == j));
You do not need a loop for the conversion:
QD_month = reshape(seno_m(:, 2), 12, []).';
wow, Jan you´re awesome, I´m feel so happy doing that. Thank you, you taught me a lot. I would like to learn more! I´m pretty sure that matlab should has amazing things.
Gracias!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 11 Apr 2021

Commented:

on 13 Apr 2021

Community Treasure Hunt

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

Start Hunting!