How to find when the value is 0 in an array?

119 次查看(过去 30 天)
Im plotting a graph of X against Y and i want to know the value of X when Y = 0. I have tried to use the find command and doing find(Y==0) but because in the array there is no exact zero number (goes to 0.00024 then to negative) it wont return anything.
Y = (U_y .* time) - (0.5 .* g .* (time).^2) + handles.Height;
Index = find(Y==0)
X_point = X(Index)

回答(2 个)

Rik
Rik 2018-12-5
This answer contains code to find the zero crossing in a vector
https://www.mathworks.com/matlabcentral/answers/267222-easy-way-of-finding-zero-crossing-of-a-function#answer_209072

Walter Roberson
Walter Roberson 2018-12-5
[~, Index] = min(abs(Y));
The zero crossing will be between the point at Index and the next point over in one of the two directions.
You could also use
mask = Y > 0;
% goes from positive to negative ?
Index = find(Y(1:end-1) & ~Y(2:end));
if isempty(Index);
% goes from negative to positive ?
Index = find(~Y(1:end-1) & Y(2:end));
end
if isempty(Index)
%no zero crossing
end
  2 个评论
Greg Simmons
Greg Simmons 2022-8-8
very clever!... this second script helped me a lot. Thanks Walter.
Walter Roberson
Walter Roberson 2022-8-8
The script should probably be
mask = Y > 0;
% goes from positive to negative ?
Index = find(mask(1:end-1) & ~mask(2:end));
if isempty(Index);
% goes from negative to positive ?
Index = find(~mask(1:end-1) & mask(2:end));
end
if isempty(Index)
%no zero crossing
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by