How to change the positions of elements in a vector?

12 ビュー (過去 30 日間)
federico moro
federico moro 2017 年 5 月 15 日
コメント済み: kowshik Thopalli 2017 年 5 月 15 日
Hi all, I need to find all the components of a vector that are equal to 3 and move them at the end of the vector. Any suggestion? Thanks

回答 (2 件)

Guillaume
Guillaume 2017 年 5 月 15 日
v = [v(v ~= 3), v(v == 3)]; %assuming a row vector. If column vector replace , by ;

kowshik Thopalli
kowshik Thopalli 2017 年 5 月 15 日
Using the find() function you can do this very easily.
[R,c]= find(myVector==3)
This gives you the positions of the elements which are equal to 3. Once you have this you can easily reorder. Hope this helps
  2 件のコメント
federico moro
federico moro 2017 年 5 月 15 日
Thanks but then how can I move them to end? It is a long vector I can not do this one by one..
kowshik Thopalli
kowshik Thopalli 2017 年 5 月 15 日
The answer by Guillaume works. I would not have done it As elegantly as that answer

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

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by