I'm trying to create a code that doing something like this ......

1 view (last 30 days)
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

Accepted Answer

Guru
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));

More Answers (1)

Jan
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);
  1 Comment
Javier
Javier on 5 Jul 2013
Thanks Jan Simon, but I think that my MEX version not support your code "RunLength =( " Warning: You are using gcc version "4.6.3-1ubuntu5)". The version currently supported with MEX is "4.3.4". For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release/

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!