sort vector elements under given restrictions

2 visualizaciones (últimos 30 días)
oxy
oxy el 19 de Mzo. de 2014
Comentada: zepp el 20 de Mzo. de 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...

Respuesta aceptada

zepp
zepp el 19 de Mzo. de 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)]

Más respuestas (1)

oxy
oxy el 20 de Mzo. de 2014
Editada: oxy el 20 de Mzo. de 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 comentario
zepp
zepp el 20 de Mzo. de 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)];

Iniciar sesión para comentar.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by