If Else Statement in For Loop

2 次查看(过去 30 天)
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

采纳的回答

Chunru
Chunru 2021-9-3
n1 = 6; % 1440
n2 = 4; % 721
n3 = 2; % 40
mag_strength = randn(n1, n1, n3)
mag_strength =
mag_strength(:,:,1) = -0.8025 0.9344 1.7522 -0.0025 0.7267 0.4267 -0.7882 0.1544 -1.3793 -1.1197 1.3624 -0.3141 -0.7260 1.9888 1.1160 -1.9104 0.4341 -1.6269 1.6487 1.2641 -0.9046 -1.8453 -0.3283 0.8055 -0.7476 -0.2142 0.9903 0.0892 -1.5335 0.3169 0.5660 0.1052 -2.0026 -0.0124 0.9250 0.4308 mag_strength(:,:,2) = -0.2306 -0.0103 -1.3022 0.6091 -0.1025 2.0710 -0.1591 -0.1148 0.0871 -1.6085 -0.6099 0.9604 0.1962 -1.2370 0.1842 0.9875 -0.0267 0.5523 1.5947 -0.9226 1.1566 -0.1529 0.5219 -1.0731 0.4503 -0.5973 1.5207 0.2089 -0.3907 -0.8328 1.0511 0.7779 -0.3594 -0.0712 0.0253 -0.7505
Fronts = mag_strength;
Fronts(Fronts<=1) = nan;
Fronts
Fronts =
Fronts(:,:,1) = NaN NaN 1.7522 NaN NaN NaN NaN NaN NaN NaN 1.3624 NaN NaN 1.9888 1.1160 NaN NaN NaN 1.6487 1.2641 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Fronts(:,:,2) = NaN NaN NaN NaN NaN 2.0710 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 1.5947 NaN 1.1566 NaN NaN NaN NaN NaN 1.5207 NaN NaN NaN 1.0511 NaN NaN NaN NaN NaN
  1 个评论
Brian DeCicco
Brian DeCicco 2021-9-3
Thank you, this makes a lot more sense! I was really over complicating my approach here.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by