public static Vector Rrowsum_matrix(Vector x, int ncol, AtomicVector groups, AtomicVector ugroup, boolean naRm) {
int numGroups = ugroup.length();
Matrix source = new Matrix(x, ncol);
MatrixBuilder result = source.newBuilder(numGroups, ncol);
for(int col=0;col!=ncol;++col) {
// sum the rows in this column by group
double groupSums[] = new double[numGroups];
for(int row=0;row!=source.getNumRows();++row) {
int group = ugroup.indexOf(groups, row, 0);
groupSums[group] += source.getElementAsDouble(row, col);
}
// copy sums to matrix
for(int group=0;group!=ugroup.length();++group) {
result.setValue(group, col, groupSums[group]);