Rowsum or columnsum of a matrix


I have a naive question. I am working on a hierarchical multinomial model with individual-level coefficients. I have N people, K outcomes per choice set, different people have different number of choice sets and Z attributes.

I find myself needing to calculate this operation: I would element-multiply a N x Z matrix with another N x Z matrix, and then sum the columns to get to a N x 1 vector.

Is there a operator to perform the columnsum? I apologize if it is already in the manual. I could not seem to find it.


There is no such function. You could pre-multiply the thing by a row_vector of ones.

Late follow-up, but it shouldn’t be hard to just write a row sum or column sum function in Stan. Input would be a matrix and output a vector.