How to use if statement to add if negative and minus if positive

16 ビュー (過去 30 日間)
pkll201
pkll201 2022 年 5 月 30 日
コメント済み: Stephen23 2022 年 5 月 30 日
I am trying to write an if statement to add 180 if the value is negative, and minus 180 if the value is positive. I have tried the below script and it does not work! I do not know how to fix this, and was hoping someone might have an idea? get a bit stick on if statements so find it difficult to problem solve! I have also attached the bit of code beforehand for context
%% Zeros to NaN
P5LANK=PLANK;
P5LANK(P5LANK==0) = NaN;
%% Unwrap so doesn't jump at 180 degrees
P5LANK_unwrap=unwrap(P5LANK*pi/180)*180/pi;
[m,n] = size(P5LANK_unwrap);
%% Interpolate
[Ny,Nx] = size(P5LANK_unwrap);
Nz = sum(~isnan(P5LANK_unwrap)); % Determine number of non-NaN
for k = 1:Nx
P5LANK_interp(:,k) = interp1(1:Nz(k),P5LANK_unwrap(1:Nz(k),k),1:Ny,'linear','extrap');
end
%% Minus 180
for k = 1:Nx
if
P5LANK_interp(:,k) > 0
P5LANK_minus180 = P5LANK_interp(:,k) - 180;
else
P5LANK_minus180 = P5LANK_interp(:,k) + 180;
end
  2 件のコメント
Torsten
Torsten 2022 年 5 月 30 日
You know that
P5LANK_interp(:,k) > 0
is only true if all elements in the k-th column are >0 ?
Stephen23
Stephen23 2022 年 5 月 30 日
Perhaps MOD does what you want.

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

採用された回答

dpb
dpb 2022 年 5 月 30 日
PM180=P-sign(P)*180;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by