I'm trying to create a code that doing something like this ......
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
Guru
el 4 de Jul. de 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 comentarios
Más respuestas (1)
Jan
el 5 de Jul. de 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);
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!