Please suggest a method using Vectorization.

조회 수: 3 (최근 30일)
Vinod S
Vinod S 2012년 10월 13일
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일
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개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by