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 commentaires

Jan
Jan le 4 Déc 2017
The description is neither clear nor unique. How is "run" exactly defines?
Hamad Alsayed
Hamad Alsayed le 4 Déc 2017
On reflection, this would amount to measuring the absolute value of the vertical distance between each peak and trough (0 first element .... 3 fifth element ... 1 eighth element ...). So a "run" is defined as each one of those individual arcs. I hope this clears it up for you. I see from other Answers that the function findpeaks may be useful here, but I have no access to the Signal Processing toolbox. If you require further clarification please do ask.
Jan
Jan le 4 Déc 2017
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?
Hamad Alsayed
Hamad Alsayed le 4 Déc 2017
The vector is oscillating up and down - plotting it will illustrate this clearly. So the question boils down to... "it moved up this much"... then "moved down this much"... etc
Question has now been answered. Thank you for your input, Jan.

Connectez-vous pour commenter.

 Réponse acceptée

Stephen23
Stephen23 le 4 Déc 2017
Modifié(e) : Stephen23 le 4 Déc 2017

1 vote

>> V = [0,0,1,2,3,2,2,1,2,2,1,2,3,4,3];
>> W = V([true,0~=diff(V)]); % remove repeats
>> diff(W([true,0~=diff(sign(diff(W))),true]))
ans =
3 -2 1 -1 3 -1
Take the absolute value if you need to.

1 commentaire

Hamad Alsayed
Hamad Alsayed le 4 Déc 2017
Thank you very much, Stephen. Works perfectly for me.

Connectez-vous pour commenter.

Plus de réponses (1)

Damo Nair
Damo Nair le 6 Déc 2017
Modifié(e) : Stephen23 le 6 Déc 2017

0 votes

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 commentaires

Stephen23
Stephen23 le 6 Déc 2017
Modifié(e) : Stephen23 le 7 Déc 2017
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
Damo Nair le 6 Déc 2017
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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by