필터 지우기
필터 지우기

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

조회 수: 59 (최근 30일)
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.

채택된 답변

Stephen23
Stephen23 2018년 6월 7일
편집: Stephen23 2018년 6월 8일
>> 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
Shan Langlais
Shan Langlais 2018년 6월 7일
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 2018년 6월 7일
편집: Stephen23 2018년 6월 7일
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.

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

추가 답변 (1개)

Damo Nair
Damo Nair 2018년 6월 7일
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
Stephen23
Stephen23 2018년 6월 8일
편집: Stephen23 2018년 6월 8일
"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.

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by