Sign switching in a sequence

3 vues (au cours des 30 derniers jours)
olig
olig le 7 Juin 2013
I have a column vector for example,
A=[10; 30; 30; 40; 60; 70; 50; 50; 60; 20; 20]
What I want to do is count each time a number makes a transition from a smaller number to a large number, to count the numbers of times a number makes a transition from a larger number to a smaller number and the number of times the is no change in numbers.
Any help would be great thanks!

Réponse acceptée

Doug Hull
Doug Hull le 7 Juin 2013
Look at the diff command. It will subtract each element from the next. Look at the sign of that calculation and you will be all set.
>> A=[10; 30; 30; 40; 60; 70; 50; 50; 60; 20; 20]
A =
10
30
30
40
60
70
50
50
60
20
20
>> d = diff(A)
d =
20
0
10
20
10
-20
0
10
-40
0
>> nnz(d > 0)
ans =
5
>> nnz(d < 0)
ans =
2
>> nnz(d == 0)
ans =
3

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 7 Juin 2013
Modifié(e) : Azzi Abdelmalek le 7 Juin 2013
ii=histc(sign(diff(A)),[-1;0;1])
%ii(1) correspond to switch high-low
%ii(2) no switch
%ii(3) switch low-high
doc diff
doc sign
doc histc
  1 commentaire
olig
olig le 7 Juin 2013
Thanks!

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by