Split array into separate arrays, where difference between values exceeds a specific number

Let's say I have this array.
A = [1 2 3 4 10 11 12 20 21 22 23 29 30 31 32 40 41 42];
In this array, between the 4th and 5th the array increments by 6, then between the 7th and 8th by 8, with all others by 1 and so on. I'd like to split the array up into separate arrays between the jumps that exceed 1. For this array I'm expecting an output like this.
Out1 = [ 1 2 3 4 ];
Out2 = [ 10 11 12 ];
Out3 = [ 20 21 22 23 ];
Out4 = [ 29 30 31 32 ];
Out5 = [ 40 41 42 ];

2 件のコメント

Rik
Rik 2018 年 7 月 29 日
Don't use numbered variables. Use diff, a loop and a cell array instead.
Nathaniel Werner
Nathaniel Werner 2018 年 7 月 29 日
Care to elaborate?

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

 採用された回答

Stephen23
Stephen23 2018 年 7 月 29 日
X = cumsum([true,diff(A)~=1]);
C = accumarray(X(:),A(:),[],@(m){m});

1 件のコメント

Rik
Rik 2018 年 7 月 29 日
Or let Matlab do the looping. That'll work as well. (or better)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2017a

タグ

質問済み:

2018 年 7 月 29 日

コメント済み:

Rik
2018 年 7 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by