Returning negative value of element in vector

Hi!
I need to return every second element in a vector as negative, how do I do it?
for example; v=[1 4 -0.3 0.2 4 7 14 -25 -0.003 245]
should be vnew=[1 -4 -0.3 -0.2 4 -7 14 25 -0.003 -245]

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 11월 6일
편집: Ameer Hamza 2020년 11월 6일

0 개 추천

v=[1 4 -0.3 0.2 4 7 14 -25 -0.003 245];
v(2:2:end) = -v(2:2:end)
Result
>> v
v =
1.0000 -4.0000 -0.3000 -0.2000 4.0000 -7.0000 14.0000 25.0000 -0.0030 -245.0000

댓글 수: 2

Thank you! I read the indexing beforehand to try to solve it myself but I didn't understand it well enough, thank you for the help!
I am glad to be of help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2020년 11월 6일

댓글:

2020년 11월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by