How can I find indices of a vector where the difference between the preceding and current element is x?

39 views (last 30 days)
I have a large 1*X vector, indices, of integers. The elements of the vector almost always are 1 greater than the previous elements but sometimes there's a "jump" where the current element might be 100 bigger than the preceding.
Ex:
indices = [5,6,7,8,9,134,135,136,137,138,403,404,405]
I would like to find the index before this change occurs
changes = [5,10]
This is so that I can split indices like so:
a1 = indices(1:changes(1))
a2 = indices(changes(1)+1:changes(2))
and so on.
Any help would be greatly appreciated.
EDIT: I know I could do this using a for loop but I would like to use "the MATLAB way" i.e. using the function find.

Accepted Answer

Stephen23
Stephen23 on 7 Jun 2018
Edited: Stephen23 on 8 Jun 2018
>> X = [5,6,7,8,9,134,135,136,137,138,403,404,405];
>> D = diff(find([true,diff(X)>100,true]));
>> C = mat2cell(X,1,D);
>> C{:}
ans =
5 6 7 8 9
ans =
134 135 136 137 138
ans =
403 404 405
EDIT: to allow for decreasing values/jumps as well, include abs like this:
D = diff(find([true,abs(diff(X))>100,true]));
  2 Comments
Shan Langlais
Shan Langlais on 7 Jun 2018
Thank you very much, this works and is better than what I had in mind. Would you mind explaining what find([true,diff(X)>100,true]) actually does? It looks very cryptic to me.
Stephen23
Stephen23 on 7 Jun 2018
Edited: Stephen23 on 7 Jun 2018
D = diff(find([true,diff(X)>100,true]));
diff(X)>100 % true where difference > 100, i.e. block boundaries.
[true, ,true] % ensure vector ends are marked as block boundaries.
find( ) % get positions of block boundaries.
D = diff( ) % difference of block boundary positions gets length of each block.
If you want to understand how it works I would suggest that you simply try each part with your data vector: first diff(X), then diff(X)>100, etc.

Sign in to comment.

More Answers (1)

Damo Nair
Damo Nair on 7 Jun 2018
Hi Stephen,
Sorry to nitpick, but when I tried ...
X = [5,6,7,8,9,134,135,136,70 80 20 10 137,138,403,404,405 199 230 276]; it gives
5 6 7 8 9
134 135 136 70 80 20 10
137 138
403 404 405 199 230 276
the difference between the 1st & last for the 2nd & 4th sequences are more than 100. Is this a shortcoming or wasn't the OP not interested in large breaks in the sequence?
Thanks Damo.
  1 Comment
Stephen23
Stephen23 on 8 Jun 2018
Edited: Stephen23 on 8 Jun 2018
"the difference between the 1st & last for the 2nd & 4th sequences are more than 100"
As far as I can tell the original question does not place any limitation on the differences within a sequence, only on between sequences, in fact the title states this explicitly: "How can I find indices of a vector where the difference between the preceding and current element is x?", which is exactly what my code does. If you are interested in the maximum difference within a sequence then you can ask a new question.
Note the my answers assumes that the values strictly increase: if you want to include decreasing values and jumps (i.e difference between adjacent values is less than -100), then simply add abs:
>> X = [5,6,7,8,9,134,135,136,70,80,20,10,137,138,403,404,405,199,230,276];
>> D = diff(find([true,abs(diff(X))>100,true]));
>> C = mat2cell(X,1,D);
>> C{:}
ans =
5 6 7 8 9
ans =
134 135 136 70 80 20 10
ans =
137 138
ans =
403 404 405
ans =
199 230 276
"...or wasn't the OP not interested in large breaks in the sequence?"
You would have to ask Shan Langlais about what they were interested in, I can only go by what was written in the question.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!