sort vector elements under given restrictions

1 回表示 (過去 30 日間)
oxy
oxy 2014 年 3 月 19 日
コメント済み: zepp 2014 年 3 月 20 日
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...

採用された回答

zepp
zepp 2014 年 3 月 19 日
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)]

その他の回答 (1 件)

oxy
oxy 2014 年 3 月 20 日
編集済み: oxy 2014 年 3 月 20 日
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 件のコメント
zepp
zepp 2014 年 3 月 20 日
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)];

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

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by