identifying and isolating consecutive numbers
古いコメントを表示
I have a vector, for example, A= [1 2 3 4 14 15 23 24 25 ]
and I want a code that will identify regions of consecutive numbers and separate them into their own array. ie, a code that will split A into
B = [1 2 3 4] C = [14 15] D = [23 24 25]
I would like this code to be able to work on a matrix A of variable length. Any suggestions?
Thank you!
採用された回答
その他の回答 (4 件)
Walter Roberson
2012 年 6 月 14 日
3 投票
The splits should occur at places where diff(A) has 1's . (You can find the runs of 1's by looking at diff(diff(A)).
Once you know the length of each piece, you can use mat2cell() to break up the vector into cell arrays. (Writing to individual variables is not a good practice for something like this.)
1 件のコメント
Diego Tasso
2012 年 6 月 14 日
Follow what Mr. Walter Roberson said he is most correct.
Diego Tasso
2012 年 6 月 14 日
0 投票
Use regexp to do this, something like ( but not exactly):
[B] = regexp(A,'[1-4]','match')
repeat for C and D replacing [1-4] with the number ranges you want to isolate...this might work...not sure.
2 件のコメント
Diego Tasso
2012 年 6 月 14 日
By the way this is the cheap inefficient way to get this done...I am sure you can create a loop to do so for you.
Guillaume
2015 年 10 月 8 日
regexp is a cheap and efficient way of locating patterns in strings. It does not apply to numbers.
A loop is the most inefficient way of dealing with the problem.
Frank Uhlig
2020 年 5 月 5 日
編集済み: Frank Uhlig
2020 年 5 月 5 日
0 投票
Here is a simple sequence that gives the adjacent integers without the non-adjacent ones:
Strat with k = [ 1 2 3 4 7 8 9 12 13 140],
>> k = [ 1 2 3 4 7 8 9 12 13 140],
k =
1 2 3 4 7 8 9 12 13 140
>> i = find(diff(k) == 1),
i =
1 2 3 5 6 8
>> all = unique(sort([k(i),k(i)+1]))
all =
1 2 3 4 7 8 9 12 13
And you have all adjacent integer groups united in ascending order.
Sorting the last output into individual adjacent integer groups now is another problem.
I know this is a very late answer but wasn't sure how to implement the answer by Maziyar because it uses a variable 'D' that is not defined anywhere. I ended up writing my own and it turned out well so I thought I'd share it.
A= [1 2 3 4 14 15 23 24 25 ]
assert(size(A,1)==1 && isa(A,'double'));
p=find(diff(A)>1);
ind=[A(1),A(p+1);A(p),A(end)];
% ind =
% 1 14 23
% 4 15 25
It takes in a vector A that would have a series of sequential numbers and returns a matrix where the top rop is the start of each sequence and the bottom row is the end of each sequence.
2 件のコメント
Katerina F
2024 年 2 月 16 日
I am getting this error:
Operands to the logical AND (&&) and OR (||) operators must be convertible to
logical scalar values. Use the ANY or ALL functions to reduce operands to logical
scalar values.
Any suggestions please?
Eric Homer
2024 年 2 月 16 日
What's the data you are passing in. Can you please share what is causing the issue?
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!