Condition on zero crossing

1 次查看(过去 30 天)
letoppina
letoppina 2018-2-27
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.

回答(1 个)

lokender Rawat
lokender Rawat 2018-3-6
编辑:lokender Rawat 2018-3-6
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.

Community Treasure Hunt

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

Start Hunting!

Translated by