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

조회 수: 1 (최근 30일)
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

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by