フィルターのクリア

How to properly create for loop with if statement

13 ビュー (過去 30 日間)
Light
Light 2023 年 4 月 2 日
編集済み: Torsten 2023 年 4 月 22 日
Hi, how do you create a for or while loop with an if statement which adds 360 to an existing variable l, if l is negative?
  1 件のコメント
Stephen23
Stephen23 2023 年 4 月 2 日
移動済み: Stephen23 2023 年 4 月 20 日
The simple and efficient approach:
L = -180:20:180
L = 1×19
-180 -160 -140 -120 -100 -80 -60 -40 -20 0 20 40 60 80 100 120 140 160 180
L = mod(L,360)
L = 1×19
180 200 220 240 260 280 300 320 340 0 20 40 60 80 100 120 140 160 180

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2023 年 4 月 2 日
It is necessary to use the index on ‘L’ on both sides of the equation.
L = -180:20:180
L = 1×19
-180 -160 -140 -120 -100 -80 -60 -40 -20 0 20 40 60 80 100 120 140 160 180
for l = 1:length(L)
if L(l) < 0
L(l) = L(l) + 360;
end
end
L
L = 1×19
180 200 220 240 260 280 300 320 340 0 20 40 60 80 100 120 140 160 180
A one-line version (avoiding the loop) would be —
L = -180:20:180
L = 1×19
-180 -160 -140 -120 -100 -80 -60 -40 -20 0 20 40 60 80 100 120 140 160 180
L(L<0) = L(L<0) + 360
L = 1×19
180 200 220 240 260 280 300 320 340 0 20 40 60 80 100 120 140 160 180
The one-line version is more efficient.
.
  91 件のコメント
Light
Light 2023 年 4 月 21 日
mag_v or |v| (magnitude of v) is given as: sqrt((v0*cos(theta).^2) + (v0*sin(theta).^2))
But the formula for the distance using x and y is: s = (integral) sqrt(1+ ((dy/dt)/(dx/dt))^2) dx
sorry for the confusion. The first is the magnitude of v while the second is the distances based on the values of x and y.
Torsten
Torsten 2023 年 4 月 22 日
編集済み: Torsten 2023 年 4 月 22 日
Sorry, but your formula for s is simply wrong. Not even the physical unit of your s-definition (which should be meters) is correct for the formula.
The distance travelled is the integral over the magnitude of the velocity.

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by