Condition on zero crossing
    4 views (last 30 days)
  
       Show older comments
    
Hi everybody,
I need to make an if statement on a zero crossing of an array (composed of n variable). In particular, I need to find when my array crosses the zero and based on that moment make the if statement. For example:
if array == 0 (that states for: when my array crosses zero)
 bla bla bla
else
 bla bla bla
end
(the problem is that in my array there is not a value that corresponds exactly to zero).
P.S. Sorry for my lack of precise information, I'm new in matlab.
0 Comments
Answers (1)
  lokender Rawat
      
 on 6 Mar 2018
        
      Edited: lokender Rawat
      
 on 6 Mar 2018
  
      Since you mentioned that there is no single value in the array which equals zero. And you specifically want to do some task when there is a zero crossing and other task when it does not. So I have included a sample script below:
% numZeroCross() will return the number of zero-crossing in the array
% passed as parameter 
function times=numZeroCross(x)
len=length(x);
count=0;
   for i=1:len-1
      if((x(i)*x(i+1)) < 0)
        disp('zero crossing');
        %do something
        count=count+1;
      else
        disp('not a zero crossing');
        %do something
      end
   end
times=count;
end
For example the array has values:
arr = [80.6 120.8 -115.6 -76.1 131.3 105.1 138.4 -81.3-95.3 89.2 -154.1 121.4 -85.1 96.8 68.2]
Call the function from the command window : numZeroCross(arr)
Output will be 8.
0 Comments
See Also
Categories
				Find more on Logical in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
