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

1 次查看(过去 30 天)
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

采纳的回答

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

更多回答(1 个)

Jan
Jan 2013-7-5
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 个评论
Javier
Javier 2013-7-5
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/

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by