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

1 Ansicht (letzte 30 Tage)
Javier
Javier am 4 Jul. 2013
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

Akzeptierte Antwort

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

Weitere Antworten (1)

Jan
Jan am 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 Kommentar
Javier
Javier am 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/

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB Compiler finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by