Why does comparison tool show modification when the values look the same?
6 次查看(过去 30 天)
显示 更早的评论
I'm comparing two .mat files, each containing a 2D matrix (2416 x 843) which should be identical. The left one is generated by a MATLAB script and weighs 585 KB. The right one by a Python script and weighs 550 KB. The MATLAB comparison tool highlights some values (including all NaNs, by the looks of it) as modified, though they appear to be the same (see screenshot). This only seems to happen above a certain volume of data (each value in this matrix represents the average of 9 values; the issue does not occur for averages of 8 values or fewer). This does not happen with any other files/variables in this workflow.
Does anyone have any ideas why this might be happening?

Edit to add: I have also tried loading these two variables in the command window and compare them in a few different ways (examples below), all of which seem to suggest that MATLAB identifies them as equal.
>> sum(meanSampleAge - py_meanSampleAge, "all", "omitnan")
ans =
0
>> py_meanSampleAge(1642, 550) == meanSampleAge(1642, 550) % highlighted cell in the image above, same for others
ans =
logical
1
0 个评论
回答(2 个)
Taylor
2024-4-18
It could be a data type issue. Double check what data type Python is saving as (MATLAB defaults to double precision). Here is an example of how sneaky data precision issues can be.
Pi is equal to pi of course
pi == pi
But what if we use different precisions of pi?
double(pi) == single(pi)
Well let's just convert that single-precision pi back to double
double(pi) == double(single(pi))
So if your data is saved by Python as single and MATLAB tries to automatically convert it to double, they values may still not show up as "equal".
4 个评论
Taylor
2024-4-19
Interesting. I'm hoenstly not entirely sure what is happening here. The NaNs being highlighted makes sense because NaNs are undefined values.
nan == nan
nan ~= nan
This is why isequaln exists. I'm not sure why the numeric values are being identified as not equal/modified. You could do more digging by inspecting the values at a specific index of the two matrices. You could also look at passing the data directly from Python to MATLAB using the MATLAB Engine in Python.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!