MATLAB: Does the numberOfNonZeros method from the MWNumericArray class in the MATLAB Builder for JA 2.0.2 (R2008b) not behave as expected

MATLAB Compiler SDK

I am using the method: numberOfNonZeros from the MWNumericArray class in the MATLAB Builder for JA. The documentation for this method, derived from the following location:
Reads as follows:
Returns:
Current number of non-zero elements in a sparse array.
I am using this method inside a Java program as follows:
import com.mathworks.toolbox.javabuilder.*;
class test{
public static void main(String[] args)
{
MWNumericArray m = MWNumericArray.newSparse(5,5,5,MWClassID.DOUBLE, MWComplexity.REAL);
m.set(new int[]{1, 1 }, 0);
m.set(new int[]{1, 2}, 0);
m.set(new int[]{1, 3}, 0);
System.out.println("Number of non zeros: " + m.numberOfNonZeros());
}
}
When I compile and run this code, I expect it to give me the number: n-3, where 'n' is the total number of elements in the sparse array. However, I always receive the number: m, where 'm' is the number of elements that I have assigned to 0 in the sparse matrix. For example, the result of the above program is:
Number of non zeros: 3

Best Answer

The method: numberOfNonZeros, returns the number of elements that could potentially be nonzero and not the actual number of non-zero elements. Therefore, the number that you see, is the number of elements for which the sparse matrix has allocated storage.
This behavior is captured in the documentation for the MATLAB Compiler for the numberOfNonZeros function:
<http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/mwsizenumberofnonzerosconst.html>