If Else Statement in For Loop
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Brian DeCicco
el 2 de Sept. de 2021
Comentada: Brian DeCicco
el 3 de Sept. de 2021
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 comentarios
Respuesta aceptada
Chunru
el 3 de Sept. de 2021
n1 = 6; % 1440
n2 = 4; % 721
n3 = 2; % 40
mag_strength = randn(n1, n1, n3)
Fronts = mag_strength;
Fronts(Fronts<=1) = nan;
Fronts
Más respuestas (0)
Ver también
Categorías
Más información sobre Multidimensional Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!