How to change the positions of elements in a vector?
8 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Guillaume
2017-5-15
v = [v(v ~= 3), v(v == 3)]; %assuming a row vector. If column vector replace , by ;
0 个评论
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 个评论
kowshik Thopalli
2017-5-15
The answer by Guillaume works. I would not have done it As elegantly as that answer
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!