If Else Statement in For Loop
2 views (last 30 days)
Show older comments
Hello, I have a 1440,721,40 matrix called mag_strength. The first dimension is longitude, the second is latitude, and the third represents 40 time steps of data. I'm trying to create a for loop with an if / else statement embedded that will look at all the data at each time step of the 3rd dimension and keep all values >1 and replace all other values that don't meet the requirement to NaNs. My resulting matrix (of equal size) is named: Fronts. When I run the loop, I get all NaN values, when I know that I should get a mixture of NaNs and positve values greater than 1. Any ideas on what's going on?
Fronts = zeros(1440,721,40);
for F = 1:40
disp(F)
if mag_strength(:,:,F)>1
Fronts(:,:,F) = mag_strength(:,:,F);
else
Fronts(:,:,F) = NaN;
end
end
0 Comments
Accepted Answer
Chunru
on 3 Sep 2021
n1 = 6; % 1440
n2 = 4; % 721
n3 = 2; % 40
mag_strength = randn(n1, n1, n3)
Fronts = mag_strength;
Fronts(Fronts<=1) = nan;
Fronts
More Answers (0)
See Also
Categories
Find more on Multidimensional Arrays 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!