I'm trying to create a code that doing something like this ......
1 view (last 30 days)
Show older comments
Hello........ I'm trying to create a code that doing something like this
data=[1 1 1 -1 1 1 -1 -1 -1 1 1 1]
take the sequence of positive and negative data
= 3 -1 2 -3 3
and, take the position of the value when it changes from positive to negative
= 1 4 5 6 7 10
%----------------------------
I made this code, but only works for positive numbers....... =(
data=[1 1 1 -1 1 1 -1 -1 -1 1 1 1]
z = v > 0;
id = find([true;diff(v.') ~= 0]);
k = diff([id;numel(v)+1]);
out = k(z(id));
Thanks for your help
0 Comments
Accepted Answer
Guru
on 4 Jul 2013
You had the answer, just had to create a variable of the negative values. I am assuming that
v = data
So just change your code to this
data=[1 1 1 -1 1 1 -1 -1 -1 1 1 1]
z = v > 0;
id = find([true;diff(v.') ~= 0]);
k = diff([id;numel(v)+1]);
% Set all values as if they were negative 1, then assign those that are not
out = -k;
out(z(id)) = k(z(id));
0 Comments
More Answers (1)
Jan
on 5 Jul 2013
data = [1 1 1 -1 1 1 -1 -1 -1 1 1 1];
[v, n] = RunLength(data);
neg = v < 0;
n(neg) = -n(neg);
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!