フィルターのクリア

How to replace every other element in an array with a different number

7 ビュー (過去 30 日間)
EA
EA 2020 年 9 月 5 日
編集済み: Stephen23 2020 年 9 月 6 日
I have a vector that I am trying to turn every other entry in the array to be negative.
So it would look like x = [200 -198 196 -194 ... -2 0]
This is what I have. Is there a different approach I should be taking?
x = [200:-2:0]
x(2:2:end) = ()
  1 件のコメント
Stephen23
Stephen23 2020 年 9 月 6 日
編集済み: Stephen23 2020 年 9 月 6 日
"I have a vector that I am trying to turn every other entry in the array to be negative."
What should happen if a value is already negative? Is the intent to simply negate values regardless of their signs, or to actually negate absolute values (which matches what you described). So far it is not clear.

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

採用された回答

Stephen23
Stephen23 2020 年 9 月 6 日
>> V = 200:-2:0;
>> V(2:2:end) = -V(2:2:end)

その他の回答 (1 件)

David Hill
David Hill 2020 年 9 月 5 日
a=yourVector.*(-ones(size(yourVector))).^(0:length(yourVector)-1);

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by