Find order of increasing integers

1 回表示 (過去 30 日間)
Sebastian Daneli
Sebastian Daneli 2021 年 6 月 15 日
コメント済み: Sebastian Daneli 2021 年 6 月 15 日
I have a vector
[3,4,8,9,10,13]
How can I sort this into
[3,4]; [8,9,10]; [13];
in an efficient way? I.e., split the parts of the vector where there is a linear increase of +1 from the previous entry.

採用された回答

Stephen23
Stephen23 2021 年 6 月 15 日
編集済み: Stephen23 2021 年 6 月 15 日
V = [3,4,8,9,10,13];
D = diff(find([true,diff(V)~=1,true]));
C = mat2cell(V,1,D)
C = 1×3 cell array
{[3 4]} {[8 9 10]} {[13]}
  1 件のコメント
Sebastian Daneli
Sebastian Daneli 2021 年 6 月 15 日
Thx, works great.

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

その他の回答 (0 件)

カテゴリ

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