Please suggest a method using Vectorization.

I want to do the following operation using vectorization technique of Matlab. I am a novice user of matlab. So please tell me how i can get the following operation done without using the FOR Loop.
x=[1 2 3 4 5 6 7 8 9 10 11 1 2];
for i=1:length(x)
x(i)=((-1)^i)*x(i);
end

 採用された回答

Matt Fig
Matt Fig 2012 年 10 月 13 日
編集済み: Matt Fig 2012 年 10 月 13 日

0 投票

Many ways to do it.
Here is one:
x = x.*(-1).^(1:length(x))
and here is another:
x(1:2:end) = -x(1:2:end)

1 件のコメント

Vinod S
Vinod S 2012 年 10 月 14 日
Oh..So easy..I didn't know.Thanks Man

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

製品

タグ

質問済み:

2012 年 10 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by