How to declare and use integer matrices

For the 2nd question how do you get int k = max(y) to work? Do I need to do max(max(y[,1]),max(y[,2])) ?

Sorry I missed that one. Those reductions (min/max/etc) are only defined over one-dimensional arrays, so in your case, a workaround would be:

int k = max(to_array_1d(y))

The to_array_1d call will induce a copy, so if you want to maximise performance you could move this to the transformed data block.

If you don’t mind I have one other question… If I have vector ff(vector a, row_vector[] X) I see that I must use int m = num_elements(a) to obtain the length of the vector a . To get the number of columns of X I would have thought that int p = cols(X) would have worked, but I get no matches for cols(row_vector[ ]) ... available signatures for cols .. cols(row_vector) . Any pointers appreciated.

Because X is a one-dimensional array of row-vectors (row_vector[]), there isn’t a concept of rows/columns of an array to count. As Stan doesn’t (yet) allow ragged arrays, every row vector in the array will have to have the same number of columns, so you can use:

int p = cols(X[1])

If instead you wanted the number of row vectors in the array, you can use size(X)

1 Like