Is there an elegant way to do a "flip-flop" sort from the middle or near the middle?

1 回表示 (過去 30 日間)
Hi!
I have a sequence like 1 2 3 4 5 6 7 (it may be any number, odd or even).
I would like to sort it so that it starts with the 4 position, and goes up on either side in a "flip flop" manner: 6 4 2 1 3 5 7, or flipping it the other way 7 5 3 1 2 4 6.
Is there a "nice" way to do this? I just need to specify the starting position, and then generate the order.
Thanks!
Doug
  1 件のコメント
Douglas Anderson
Douglas Anderson 2013 年 12 月 12 日
Just to clarify -- I say "starts with the 4" position, but it can be any position, obviously not either 1 or 7! THAT I can do!

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

採用された回答

Sean de Wolski
Sean de Wolski 2013 年 12 月 12 日
n = 7;
v = [n:-2:1, 2:2:n]
  2 件のコメント
Douglas Anderson
Douglas Anderson 2013 年 12 月 12 日
Very nice! Thank you!
Douglas Anderson
Douglas Anderson 2013 年 12 月 12 日
Although that doesn't work for even numbers:
v = [n:-2:1,1:2:n-1]

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by