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.
2 件のコメント
採用された回答
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 件のコメント
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
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
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 Center および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!