Count the number of trend changes in a vector.

2 次查看(过去 30 天)
I need to count the number of times a vector increases/decreases. E.g
vec = [0 0 1 2 4 4 2 0 1 2 3]
diff(vec)
ans =
[0 1 1 2 0 -2 -2 1 1 1]
would evidently yield 4 trend changes. I don't wish to count the zero as a trend.
Tips please!
Update:
If you were to plot(vec) it's easier to see what I mean. I wish to simply count the number of row changes in the plot. A flat line (derivative of 0) isn't considered a row change in my application.
Take this as an example:
vec = [3 3 3 5 5 4 3 2 4 5 5 5];
plot(vec)
As you can see from the plot, it's 4 row changes (even though it's 8 in pure diff).
Update:
Ok, I'll try to do better.
Imagine a matrix that describes position over time, where row is position and column is time. When going in a straight line (along a single row) I wish to do nothing. But when changing course over to another row-line I wish to count each new "course" as an occurrence.
E.g
  • Travelling at row 5 and then moving up to row 6 and then going straight, that's one course change.
  • [5 5 5 6 6 6] => 1 "course change"
  • Travelling at row 5 and then moving up to row 7 is similarly one change.
  • [5 5 5 7 7 7] => 1 "course change"
  • Travelling at row 5, moving up to 7 and then directly back to row 6 is then two changes.
  • [5 5 5 7 6 6 6] => 2 "course changes"
  • Travelling at row 5, moving up to 7 and then to 12 and then going straight is two changes as the row increment is 2 and then 5 (not a straight course).
  • [5 5 5 7 12 12 12] => 2 "course changes"
So basically I wish to count each time you change position (or row) in a straight course.
  4 个评论
Jan
Jan 2012-7-16
Please, Sebastian, add informations, which are necessary to define the question, by editing the question, not as comment.

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2012-7-16
编辑:Andrei Bobrov 2012-7-16
EDIT
a = diff(vec(:));
out = nnz(unique(cumsum([true;diff(a)~=0]).*(a~=0)));
  5 个评论
Andrei Bobrov
Andrei Bobrov 2012-7-17
编辑:Andrei Bobrov 2012-7-17
Hi Sebastian! Thank you for "Accepted..".
Let
vec = [-3 -2 3 3 2 0 0 1 2 5]';
Our goal is to determine the number of groups of identical nonzero elements located near in a:
a = diff(vec);
In our case: out = 6.
t - indexs of all groups (with zeros's groups).
t = cumsum([true;diff(a)~=0]);
non-zero elements in a:
t1 = t(a~=0); % or t1 = cumsum([true;diff(a)~=0]).*(a~=0);
index of groups with none-zero element ( t21 ):
t21 = unique(t1); % or t22 = unique(cumsum([true;diff(a)~=0]).*(a~=0));
and lost:
out1 = numel(t21); % or out2 = nnz(unique(cumsum([true;diff(a)~=0]).*(a~=0)));

请先登录,再进行评论。

更多回答(2 个)

Doug Hull
Doug Hull 2012-7-12
You would need to remove the zeros from the first diff vector and diff it again, then count number of non-zeros. The only complication is the case where the first diff vector looks like:
[1 1 0 1 1]
Once the zero is removed, it looks constant. That can be dealt with, but would be messy.
  1 个评论
Sebastian Holmqvist
Good idea. But it doesn't work with this example:
>> vec = [3 3 3 5 5 4 3 2 4 5 5 5];
>> d_vec = diff(vec)
d_vec =
0 0 2 0 -1 -1 -1 2 1 0 0
>> d_vec(d_vec==0) = []
d_vec =
2 -1 -1 -1 2 1
>> diff(d_vec)
ans =
-3 0 0 3 -1
>> sum(diff(d_vec)~=0)
ans =
3
Which would be 3 changes with your argumentation. But with my (somewhat fuzzy) specification there should be 4. plot(vec) should make it more clear.

请先登录,再进行评论。


Sebastian Holmqvist
I think I've cracked it. My logic follows from;
  1. I wish to count the number of first derivative changes.
  2. I wish to exclude derivatives == 0.
  3. Derivative element trends are to be treated as single elements.
First off, I wrote a small function to remove all local dupes.
function vec = rmDupes(vec)
% Remove all duplicate subsequential elements in a vector
% Iterate backwards to avoid changing indexing
for i = length(vec):-1:2
if vec(i) == vec(i-1)
vec(i) = [];
end
end
By not removing the zero elements before removing the dupes I don't get the case where [1 2 3 3 4 5] counts as one.
vec = [1 1 2 2 3 4 1 1]
d_vec = diff(vec)
d_vec = rmDupes(d_vec)
sum(d_vec ~= 0)
ans =
3
If anyone can detect any flaws in my solution, or has a better suggestion, I would be more than grateful if you would give me some feedback!

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by