sort vector elements under given restrictions

2 vues (au cours des 30 derniers jours)
oxy
oxy le 19 Mar 2014
Commenté : zepp le 20 Mar 2014
Hi guys,
i have the vector v=[1 2 3 4 5]
wanna resort it so that v(3) and v(5) comes first:
v=[3 5 others] % 'others' has any order
How can i do that? The problem here is exactly this: how to write 1:5 but missing 3 and 5 .
Any idea? thx...
-----------------------------------------------------------------------------------------
PS: this question is related to
The point relating to this cited question is that i can find all combinations (just for-loop), but i cannot arrange the rest of the dimensions. Thus this present question.
Thx...

Réponse acceptée

zepp
zepp le 19 Mar 2014
You can create a logical array (same length as v) with 1's for the positions you want (say, 3 and 5) and 0's for the rest and use that to refer to the indices.
v = [1 2 3 4 5]
ind = [0 0 1 0 1]
v = [v(ind==1) v(~ind==1)]

Plus de réponses (1)

oxy
oxy le 20 Mar 2014
Modifié(e) : oxy le 20 Mar 2014
Great! Thx!
Just another question: This is the way I've been doing it on octave. I wander why it doesnt work in matlab!!! :-/
vector2sort= 1:6
n=length(vector2sort)
a=2; b=4 % i.e. I wanna the 2nd and 4th elements of vector2sort first
[a b (1:n)( (1:n)!=a & (1:n)!=b )] % this is how we sort it
Why it does not work in matlab?!! Is there a way to do it more simila?
thx 4 your wisdom!
  1 commentaire
zepp
zepp le 20 Mar 2014
The concept is fine, but you can't reference array elements like that in Matlab.
Try this out:
v = 1:6;
a = 2; b = 4;
sortedv = [a b v(v~=a & v~=b)];

Connectez-vous pour commenter.

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by