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

1 view (last 30 days)
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

Accepted Answer

Steven Lord
Steven Lord on 29 May 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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!