Info

This question is closed. Reopen it to edit or answer.

Help. I'm trying to created a code that...........

1 view (last 30 days)
Javier
Javier on 16 Jul 2013
Closed: MATLAB Answer Bot on 20 Aug 2021
I'm trying to created a code that doing something like this, when v (time serie) changes only from negative to positive (zero crossings),this was separated by at least 4 elements from other zero crossings(left and right direction), and if I take 8 elements (from zero crossings)to the right, at least the 80% of the elements were positive,and if i take 6 elements to the left (from zero crossings), at least the 60% of the elements were negative. (I have a huge data serie , and I'm trying to generate a code that I can rearrange the parameters.
Regards)
v=[-1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 1 1 1 -1 -1 1 1 1 1 -1 -1
1 -1 -1 1 1 1 1 1 1 1 1 -1 1]
regards,
  4 Comments
Jan
Jan on 17 Jul 2013
How large is "huge"? It matters if you want to process 1GB of data or only some MB.
Javier
Javier on 17 Jul 2013
Edited: Javier on 17 Jul 2013
Hello Jan, are only a few mb...
>> size(v)
ans =
1 180407
regards

Answers (2)

Andrei Bobrov
Andrei Bobrov on 16 Jul 2013
a = v == 1;
ii = [true;diff(a(:)) ~= 0];
i0 = find(ii);
iii = reshape(diff([i0;numel(a)+1]),2,[])';
i1 = reshape(i0,2,[])';
indexout = i1(all(bsxfun(@le,[.6 .8],bsxfun(@rdivide,iii,[6 8])),2),2);
  1 Comment
Javier
Javier on 17 Jul 2013
Edited: Javier on 17 Jul 2013
Thank you so much Andrei, but for example, if I wanna (rearrange the parameters) change this part of the code("when v (time serie) changes only from negative to positive (zero crossings),this was separated by at least 4 elements from other zero crossings(left and right direction)"), change 4 by 10, or other number
regards

Jan
Jan on 17 Jul 2013
Edited: Jan on 17 Jul 2013
Try a simple FOR loop at least as a proof of concept: (See FEX: RunLength)
[value, rep, index] = RunLength(v);
n = numel(value);
match = zeros(1, n); % Pre-allocate
iMatch = 0;
first = find(index > 6, 1, 'first'); % Or <= ?
last = find(index < numel(v) - 8, 1, 'last'); % Or <= ?
for k = first:last
if n(k-1) < 4 && n(k) < 4 % Or <= ?
continue;
end
q = index(k);
if sum(v(q:q+7) == 1) / 8 < 0.8 % Or q+1:q+8 ?
continue;
end
if sum(v(q-6:q-1) == 1) / 6 < 0.6 % Or q-5:q ?
continue;
end
% All conditions match:
iMatch = iMatch + 1;
match(iMatch) = k;
end
match = match(1:iMatch);
Some points are not clear yet, e.g. if "8 elements to the right" is inclusive or exclusive the current element. But if this is cleared, e.g. the conditiosn could be vectorized easily. But it is not sure if this is faster and perhaps the speed of the loop is sufficient already.

This question is closed.

Products

Community Treasure Hunt

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

Start Hunting!