Condition on zero crossing
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!