/** * Converts a libsvm sparse matrix to a svdlibj sparse one * * @param mx the libsvm sparse matrix * @return the svdlibj matrix */ private static SMat convertLibsvmToSvdlibj(VectorNode[][] mx) { SMat S; int i, j, n; int maxColIndex = 0; // n = number of non-zero elements for (i = 0, n = 0; i < mx.length; i++) { for (j = 0; j < mx[i].length; j++) { n++; if (mx[i][j].index > maxColIndex) { maxColIndex = mx[i][j].index; } } } S = new SMat(mx.length, maxColIndex, n); for (j = 0, n = 0; j < maxColIndex + 1; j++) { VectorNode[] column = SparseVector.columnVector(mx, j); S.pointr[j] = n; for (i = 0; i < column.length; i++) { S.rowind[n] = column[i].index; S.value[n] = column[i].value; n++; } } S.pointr[S.cols] = S.vals; return S; }