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

1 vue (au cours des 30 derniers jours)
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

Réponse acceptée

Steven Lord
Steven Lord le 29 Mai 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 commentaire
Michelle De Luna
Michelle De Luna le 29 Mai 2020
Steven, thank you for your help! I truly appreciate this simple solution! Take care. :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by