Find the consecutive positive and negative elements for the entire array

Hello all, I have a channel from which I take around 1 million samples now it contains both positive and negative values in it. My intention is to find the consecutive positive and negative integers(doesn't have to be same) and perform some operations on it. I have given my code below. chA is my channel from where i derive my inputs as values. The code is only giving me a value of 43.2600, which ideally should have given an array of numbers as there are lots of samples which are consecutive positive and negative.
for i = 1:1000000
if (chA(i)<0) && (chA((i+1) >0))
tan = ((chA(i+1))- chA(i));
deltaOfTime = tan/i;
end
thanks

5 Comments

What is the wanted output for e.g.:
x = [-1, -1, 2, 3, 0, -1]
Is the 0 treated as positive?
Hi, The output should be all the values from the variable deltaOfTime. It should contain all the value after each iteration which fulfills the conditions it can be as an array/vector. I just need to have the values of deltaOfTime. yes you can consider zero as positive
Jayanta
you are almost there, all left is is
1. to accumulate the indices that your loop is already finding.
2.
include both transistions - to + and + to -
One way of doing both things would be
L=0
if ((chA(i)<0) && (chA((i+1) >0))) || ((chA(i)>0) && (chA((i+1) <0)))
L=[L i];
end
L(1)=[]
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
Hi, your answer helped me, but sorry i cant thumbs up as i think i am not eligible to do so. its disabled. Anyways thanks :)
@Jayanta Deb: You can accept and vote answers only, not your own question. When you are able to add a comment, you should have the power to vote also. Accepting an answer is useful for the forum, because the readers see, that the problem is solved.
If would be interesting to know and valuable for the forum, if these approaches are useful for your problem:
index = find(chA(1:end-1) < 0 & chA(2:end) > 0);
% Or: index = find(diff(chA < 0)); % For both directions
deltaOfTime = (chA(index + 1) - chA(index)) ./ index;

Sign in to comment.

 Accepted Answer

If you want the start indices and the lengths of the runs: With FEX: RunLength:
x = randn(1, 1e6);
[B, N, Index] = RunLength(x < 0);
negStart = Index(B);
negLen = N(B);
posStart = Index(~B);
posLen = N(~B);
[EDITED]
Perhaps you want something like this:
deltaOfTime = zeros(1, 1000000); % Pre-allocate
c = 0;
for i = 1:1000000 - 1 % -1 to support chA(i+1)
if (chA(i) < 0) && (chA(i+1) >= 0)
% if (chA(i) < 0) == (chA(i+1) >= 0) % if both directions are wanted
c = c + 1;
deltaOfTime(c) = (chA(i+1) - chA(i)) /i;
end
end
deltaOfTime = deltaOfTime(1:c); % Crop unused elements
If this solves your problem, you can "vectorize" the code to increase the speed:
index = find(chA(1:end-1) < 0 & chA(2:end) > 0);
deltaOfTime = (chA(index + 1) - chA(index)) ./ index;
Or if you want to find both transicients even simpler:
index = find(diff(chA < 0));
deltaOfTime = (chA(index + 1) - chA(index)) ./ index;
NOTE: Using "tan" as name of a variable might be confusing, if the function tan() is used later on. Better avoid shadowing the names of built-in functions.

6 Comments

sorry i dont find it relevant.
consider the array as [0,1,-3,4,5,6,7,8,9,-19]
for i = 1:1000000 % loops strats from 1 and ends at 1000000
if (chA(i)<0) && (chA((i+1) >0)) % if i = 1, i+1 = -3 <it satisfy the condition>
tan = ((chA(i+1))- chA(i)); %calculate it
deltaOfTime = tan/i; %store the value here in the vector deltaOfTime
end
now in the next iteration it should be able to find out the next consecutive positive and negative value which is 9,-19
Then I did not understand the expression "consecutive positive and negative integers". Can you create the wanted output for [0,1,-3,4,5,6,7,8,9,-19] manually?
See [EDITED] in my answer.
well that works exactly how i wanted. thanks Jan Simon
If an Answer was unaccepted, only the OP has the ability to do it during the first 7 days after a Question is posted.
Neither Jan Simon nor anyone else with Editor privileges are able to accept or unaccept Answers during that time.
No one gets Reputation Points for accepting their own Answers.
I just have an array of 76140 data. What to do, if I want to know the starting index of consecutive negative elements and also thier count. Thank you in advance

Sign in to comment.

More Answers (1)

Jayanta
you are almost there, all left is is
1.
to accumulate the indices that your loop is already finding. You current loop only keeps the last zero crossing.
and
2.
include both transitions - to + and + to -
One way of doing both things would be
L=0
if ((chA(i)<0) && (chA((i+1) >0))) || ((chA(i)>0) && (chA((i+1) <0)))
L=[L i];
end
L=[]
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG

2 Comments

I just have an array of 76140 data. What to do, if I want to know the starting index of consecutive negative elements and also thier count. Thank you in advance

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!