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

4 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Steven Lord
Steven Lord am 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

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by