Sorting duplicate incremental array elements

4 ビュー (過去 30 日間)
David
David 2014 年 7 月 9 日
編集済み: James Tursa 2014 年 7 月 9 日
I was curious if there is a more efficient method for performing the following result:
>> x = 1:4;
>> xx = sort(x([1:end,1:end]))
1 1 2 2 3 3 4 4
If an alternative method is applicable to the previous example, consider also the more general case:
>> x = 1:4;
>> n = 2;
>> xn = sort(x(repmat([1:end],[1,n])))
To me it seems like the use of the sort command is inefficient and could cause notable performance reductions when working with larger incremental lists.
I would appreciate any suggestions.

回答 (1 件)

José-Luis
José-Luis 2014 年 7 月 9 日
編集済み: José-Luis 2014 年 7 月 9 日
x = 1:4; %sort(x); %if needed
xx = [x;x];
xx = xx(:);
  1 件のコメント
David
David 2014 年 7 月 9 日
That is definitely more efficient.
It would be a little more tricky to achieve for an input, x, with an arbitrary shape (a column vector would be one such example). It seems that no matter how you approach such a problem, you will need some preliminary code to handle the dimensionality of x, such that its first dimension is of singleton length so that the duplicates can be concatenated along this dimension.
Cheers

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

カテゴリ

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