Avoid NaNs in a sum of a for loop
8 views (last 30 days)
Show older comments
Hi all! I have a problem.. here is my code..
for l=12:length(PM25)
if isnan(PM25(l)) || (isnan(PM25(l-1))) && (isnan(PM25(l-2)))
C(l)=NaN;
else
C(l) = (PM25(l)*(w(l)^0)) + (PM25(l-1)*(w(l)^1)) + (PM25(l-2)*(w(l)^2)) + ...
(PM25(l-3)*(w(l)^3)) + (PM25(l-4)*(w(l)^4)) + (PM25(l-5)*(w(l)^5)) + ...
(PM25(l-6)*(w(l)^6)) + (PM25(l-7)*(w(l)^7)) + (PM25(l-8)*(w(l)^8)) + ...
(PM25(l-9)*(w(l)^9)) + (PM25(l-10)*(w(l)^10)) + (PM25(l-11)*(w(l)^11));
end
end
Some of those multiplications results are NaN.. How can i avoid them?
Thank you!
0 Comments
Answers (1)
Ameer Hamza
on 12 Apr 2020
I suggest something like this
for l=12:length(PM25)
C(l) = sum(PM25(l:-1:l-11).*w(l).^(0:11)); % no need to write each term manually
end
C(isnan(C)) = 0; % set nan values to zero.
4 Comments
See Also
Categories
Find more on Logical 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!