Create New Array from Existing Array using For Loop and If Statement

4 visualizaciones (últimos 30 días)
Good evening everyone!
I have an array named 'longitude' that has hundreds of thousands of longitude values ranging from 30 to 390 degrees. The idea is that all values between 30 and 360 should remain the same, but values above 360 (e.g. 370, 375, 380) should undergo a process by which I subtract 360 to get a numeric value smaller than 30 (e.g. 370 - 360 = 10, or 375 - 360 = 15). Is there any way to create a new array with the same size as the original that includes ALL longitude values (those between 30 and 360, and those I subtracted 360 from to get new values less than 30) using a for loop and if statement? Any help would be greatly appreciated. Thank you in advance for any suggestions and/or tips! :)
So far, I have something like this:
new = [];
for i = 1:numel(longitude);
if longitude(i) < 360
new = new + longitude(i)
elseif longitude(i) > 360
new = new + (longitude(i)-360)
end
end

Respuesta aceptada

Steven Lord
Steven Lord el 29 de Mayo de 2020
No need for a for loop or an if statement.
x = randi([30 390], 10, 10)
y = mod(x, 360)
Depending whether or not you want y to contain 0 or 360 you may need to adjust it.
y(y == 0) = 360
  1 comentario
Michelle De Luna
Michelle De Luna el 29 de Mayo de 2020
Steven, thank you for your help! I truly appreciate this simple solution! Take care. :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements 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!

Translated by