开发者

Different results using == and find in MATLAB

开发者 https://www.devze.com 2023-03-10 23:53 出处:网络
I have created a sparse matrix using MEX and also created a sparse matrix using MATLAB. To fill in the values of the matrix i have used same formula.

I have created a sparse matrix using MEX and also created a sparse matrix using MATLAB. To fill in the values of the matrix i have used same formula. Now to check if the both the matrices are equal I used result=(A==B). result returns 1 for all开发者_StackOverflow indices, which implies that all the matrix elements are equal. But if I do find(A-B) it returns some indices, which indicates that at these indices the values are non-zero. How is this possible?

Note: When i compare the value at these indices it shows equal !


I'm guessing you have values of infinity cropping up in your matrices at the same points. For example:

>> A = Inf;
>> B = Inf;
>> A == B

ans =

     1  %# They are treated as equal...

>> A-B

ans =

   NaN  %# ...but their difference actually results in NaN...

>> find(A-B)

ans =

     1  %# ...which is treated as a non-zero value.

The discrepancy here results from the fact that certain operations involving infinity result in NaN values. You can check to see if you have any infinities in A and B by using the function ISINF like so:

any(isinf(A(:)))
any(isinf(B(:)))

And if you get a value of 1 (i.e. true), then the presence of infinities is likely the source of your discrepancy.

0

精彩评论

暂无评论...
验证码 换一张
取 消