フィルターのクリア

How to adjust a vector using an if function

1 回表示 (過去 30 日間)
Benedikt Wessel
Benedikt Wessel 2018 年 9 月 17 日
コメント済み: KALYAN ACHARJYA 2018 年 9 月 18 日
Hello every body, below you can find my code. I want to customize the vector A, so if one condition is true the vector A should be adjusted.So a 1 should be added if C is smaller than 0 and a 1 should be subtracted if C is greater than 0. For example: A=[0,0,0,2,2,2,2,....]. But every time my vector A stays the same, only containing 1. Thanks
A= ones(100,1);
B=2*rand(100,1);
C=A-B;
for k=1:length(A);
if C(k)<0
A(k)+1;
else if C(k)>0
A(k)-1;
else A(k)
end
end
end

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 9 月 17 日
編集済み: KALYAN ACHARJYA 2018 年 9 月 17 日
A=[1 2 -4 0 -10 11] %This is just a example
for k=1:length(A);
if A(k)<0
A(k)=A(k)+1;
elseif A(k)>0
A(k)=A(k)-1;
else
A(k)=A(k)
end
end
disp(A);
  4 件のコメント
Benedikt Wessel
Benedikt Wessel 2018 年 9 月 18 日
Thank you very much!
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 9 月 18 日
My Pleasure @Benedikt

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by