Is there any counterpart of MATLAB function numel in java? -
i'm trying use numel (function available in matlab) in java. implementations of function available in java?
i not sure if how want numel
work here have few versions:
version1 - arrays irregular size {{1},{2,3}}
this method iterate on elements of array counting them.
public static int numel(object array) { if (array == null) return 1;// count nulls elements since new string[10] // initialized nulls int total = 1; if (array.getclass().isarray()) { total = 0; int length = java.lang.reflect.array.getlength(array); (int index = 0; index < length; index++) { total += numel(java.lang.reflect.array.get(array, index)); } } return total; }
version2 - arrays regular size new string[2][3][4]
this method use size of first rows of different levels in array size assuming rows @ same level have same size
public static int regularnumel(object array) { if (array == null) return 1; int total = 1; if (array.getclass().isarray()) { int length = java.lang.reflect.array.getlength(array); if (length > 0) { object row = java.lang.reflect.array.get(array, 0); if (row == null || !row.getclass().isarray()) return length; else //now know row array return length * regularnumel(row); } else return 0; } return total; }
Comments
Post a Comment