Finding indices in matrix with find() function
2 次查看(过去 30 天)
显示 更早的评论
Hi all,
I'm having some difficulty using the find() function with multiple conditions. Below is a sample of what I'm referring to. LUTi and LUTj are matrices with defined values.
>> LUTi(60,240)
ans =
0
>> LUTj(60,240)
ans =
1.0000
>> [theta1 theta2] = find(LUTi == 0 & LUTj == 1)
theta1 =
Empty matrix: 0-by-1
theta2 =
Empty matrix: 0-by-1
The output I'm expecting to see is [60,240], but instead, it's returning an empty matrix. Am I inputting the 2 conditions incorrectly? Thank you in advance.
Alexis
0 个评论
回答(2 个)
Paulo Silva
2011-3-6
You are using the find function in two matrices at the same time, that's not a good idea
[row column]=find(LUTi==0)
[row1 column1]=find(LUTj==1)
1 个评论
Jos (10584)
2011-3-7
Why not?
I assume that both matrices have the same size (otherwise an error would have been trhown by AND) and Alexis is looking for the indices where both comparisons are true.
Matt Tearle
2011-3-6
I think you may have fallen for an old trap. Please enter
1 - LUTj(60,240)
and see what the result is. I'm going to take a guess that it's not 0. Given the way it says LUTj(60,240) is "1.0000", I'm guessing that the value is not exactly 1, but 1 + some roundoff. In that case, testing ==1 will return false. The solution is to test something like
abs(x - 1) < tol
where tol is some small value.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!