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