how to grab the last value in a loop?
Show older comments
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
More Answers (0)
Categories
Find more on Resizing and Reshaping 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!