How to replace elements in a vector?

I have a vector and want to replace all 2's that appear in the vector with a 3 and all 3's with a 2. I can't find a way to do this in a script without a 2 becoming a 3 then automatically becoming a 2 again.

回答 (1 件)

Thorsten
Thorsten 2015 年 10 月 21 日
編集済み: Thorsten 2015 年 10 月 21 日

0 投票

First determine the indices of the 2's and 3's, then replace them:
idx2 = v == 2;
idx3 = v == 3;
v(idx2) = 3;
v(idx3) = 2;

タグ

タグが未入力です。

質問済み:

Nat
2015 年 10 月 21 日

編集済み:

2015 年 10 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by