필터 지우기
필터 지우기

Sort sequencial data from an array into separate arrays

조회 수: 3 (최근 30일)
Leyon
Leyon 2013년 10월 21일
댓글: Cedric 2013년 10월 21일
Lets say I have an array
A = {3 4 5 6 8 9 12 15 16 17}
I want to get sequences such as
A1 = {3 4 5 6}
A2 = {8 9}
A3 = {12}
A4 = {15 16 17}
How would I approach such a problem. This issue is part of a larger problem I am solving in selecting data ranges from excel sheets. Any help would be nice. Thanks

채택된 답변

Cedric
Cedric 2013년 10월 21일
편집: Cedric 2013년 10월 21일
You can proceed as follows to create blocks of consecutive numbers:
A_num = [A{:}] ;
bStart = [0, find(diff(A_num)>1), numel(A)] + 1 ;
bSize = diff( bStart ) ;
A_block = mat2cell( A_num, 1, bSize ) ;
With that, you get
>> bStart
bStart =
1 5 7 8 11
>> bSize
bSize =
4 2 1 3
>> A_block
A_block =
[1x4 double] [1x2 double] [12] [1x3 double]
>> A_block{1}
ans =
3 4 5 6
  댓글 수: 3
Leyon
Leyon 2013년 10월 21일
Would not have thought of this solution for line 2. The logic is great for the rest of the program. Thanks.
Cedric
Cedric 2013년 10월 21일
You're welcome.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by