Finding an element = 0 in an array

84 次查看(过去 30 天)
Hi,
I have a 2 by N matrix of data that I pull from a file into Matlab that has one column of values called "X" and one column of values called "Y". I know there is one element in column "Y" that is equal to 0. I need to find what row that element is in to move forward with my program. How can I do this? I know some people use the "find" function, but I haven't been able to successfully use this. Help please!

回答(1 个)

Star Strider
Star Strider 2018-6-21
You may be encountering ‘floating-point approximation error’, explained in: Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? (link).
If that’s the problem, use a tolerance with find:
Y = [1 2 3 0.0001 4 5 -0.0009]'
Zr = find(abs(Y) < 0.01)
Zr =
4
7

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by