Counting runs in a vector
2 ビュー (過去 30 日間)
古いコメントを表示
Hi all, I have the following vector for which I would like to count "runs", i.e. successive increments in each direction.
v = [0 0 1 2 3 2 2 1 2 2 1 2 3 4 3];
The desired output would look like
out = [3 2 1 1 3 1]
.. because from the first to the fifth element, v moves 3, and from the fifth to the eighth, switches direction and retracts 2, and so on.
4 件のコメント
Jan
2017 年 12 月 4 日
Sorry, I do not understand the explanation. What is the "vertical distance", what are the "peaks" and what is "trough"?
Are you looking for local maxima and minima?
採用された回答
その他の回答 (1 件)
Damo Nair
2017 年 12 月 6 日
編集済み: Stephen23
2017 年 12 月 6 日
Hi,
I hate to trouble you, but is there any way to retrieve the index of the last value? I mean, in the above example 3 would correspond to an index of 5 & -2 to 8.
Thanks Damo.
2 件のコメント
Stephen23
2017 年 12 月 6 日
編集済み: Stephen23
2017 年 12 月 7 日
Of course, just keep track of the indices:
>> V = [0,0,1,2,3,2,2,1,2,2,1,2,3,4,3];
>> X = [true,0~=diff(V)];
>> W = V(X);
>> Y = [true,0~=diff(sign(diff(W))),true];
>> diff(W(Y)) % original answer
ans =
3 -2 1 -1 3 -1
Now you can identify the indices:
>> Z = false(size(X));
>> Z(X) = Y;
>> find(Z)
ans =
1 5 8 9 11 14 15
Note that 1 is included as it defines the start value.
Damo Nair
2017 年 12 月 6 日
Outstanding! I couldn't work out the relation between Y & V. Now that you make it so simple I feel a bit silly.
Thanks very much. Goodday Damo.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!