// [[Rcpp::export]] Rcpp::IntegerVector testSortCounts(Rcpp::IntegerVector v){ Rcpp::IntegerVector res(v.length()); Vec<int> v2 = asVec(v); Vec<int> v3 = asVec(res); sortCounts(v2, v3); return res; }
rType DecisionStump<MatType>::CountMostFreq(const arma::Row<rType>& subCols) { // Sort subCols for easier processing. arma::Row<rType> sortCounts = arma::sort(subCols); rType element; int count = 0, localCount = 0; if (sortCounts.n_elem == 1) return sortCounts[0]; // An O(n) loop which counts the most frequent element in sortCounts for (int i = 0; i < sortCounts.n_elem; ++i) { if (i == sortCounts.n_elem - 1) { if (sortCounts(i - 1) == sortCounts(i)) { // element = sortCounts(i - 1); localCount++; } else if (localCount > count) count = localCount; } else if (sortCounts(i) != sortCounts(i + 1)) { localCount = 0; count++; } else { localCount++; if (localCount > count) { count = localCount; if (localCount == 1) element = sortCounts(i); } } } return element; }
// [[Rcpp::export]] Rcpp::IntegerVector getRef(Rcpp::IntegerMatrix mat, std::string type, int nthreads=1){ //allocate another matrix with transpose dimensions as mat int ncol = mat.ncol(); int nrow = mat.nrow(); if (ncol*nrow == 0) Rcpp::stop("empty input is invalid"); Rcpp::IntegerMatrix mem_smat(ncol, nrow); Mat<int> smat = asMat(mem_smat); Mat<int> omat = asMat(mat); //sort every column #pragma omp parallel for num_threads(nthreads) for (int col = 0; col < ncol; ++col){ Vec<int> ovec = omat.getCol(col); MatRow<int> svec = smat.getRow(col); sortCounts(ovec, svec); } return colSummary(mem_smat, type, nthreads); }