MATLAB: Does the additive functionality of the SPARSE command not work as expected in MATLAB 7.5 (R2007b)

addingadditionMATLAB

I am using the following code:
S = sparse(repmat(rowIndices,3,1), ...
repmat(colIndicesl,3,1), ...
[values1; -values1; values2], ...
m, n);
and I expect to generate a sparse matrix with entries that are equal to the sum of the entries in values1, -values1, and values2, which should simply be values2. However, some entries in the generated matrix are incorrect. The entries in values1 and values2 differ by large orders of magnitude.

Best Answer

This is expected behavior due to round-off errors. SPARSE does not necessarily add the arguments in the same order in which they are specified. Therefore, depending on order of addition and the relative magnitude of the arguments, round-off errors may occur.
Please see the following URL for more information on troubleshooting floating-point arithmetic problems: