Sorting rows of a matrix in Stan

363 views
Skip to first unread message

Shamil Sadigov

unread,
Aug 12, 2014, 11:51:09 AM8/12/14
to stan-...@googlegroups.com
Dear Stan Developers,
 
I am not able to sort rows of a matrix. What is the correct syntax for sorting rows of a matrix?  I tried sort_asc(), but Stan seems to comlain about the syntax.
 
 
real bla[R, C];  
  
real orderedbla
[R,C];  
  
for (r in 1:R) {  
orderedbla
[r, ] <- sort_asc(bla[r, ]);  
}
 
does not work.
 
I searched the manual for examples, but could't figure it out.
 
Thanks for your help.
Shamil.
 

Marco Inacio

unread,
Aug 12, 2014, 12:01:48 PM8/12/14
to stan-...@googlegroups.com
The following should work:


matrix [R, C] bla;  
  
matrix [R, C] orderedbla;  
  
for (r in 1:R) {

  row_vector [C] temp;
  temp <-
sort_asc(row(bla, r)); #row function select the rth row of the matrix
  for (c in 1:C) {
    orderedbla
[r, c] <- temp[c];
  }
}


Notice that real bla[R, C]; is not a matrix, but an array of two dimensions
--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bob Carpenter

unread,
Aug 13, 2014, 12:08:29 AM8/13/14
to stan-...@googlegroups.com
You should be able to do just this:

for (r in 1:R)
orderedbla[r] <- sort_asc(bla[r]);

I worked hard to get row assignment working :-)

Now having said that, it's not efficient either way because
matrices are stored column-major. So it's better if
you take an array of vectors:

vector[C] bla[R];

or to make it more like a matrix, row vectors:

row_vector[C] bla[R];

- Bob

Marco Inacio

unread,
Aug 13, 2014, 9:10:24 AM8/13/14
to stan-...@googlegroups.com

On 14-08-13 01:08 AM, Bob Carpenter wrote:
You should be able to do just this:

  for (r in 1:R)
    orderedbla[r] <- sort_asc(bla[r]);

I worked hard to get row assignment working :-)
That's pretty cool, is assignment also possible for columns of matrices? Also, this was pretty hard to find in page 20, may I suggest that we add it the description of the row function in the manual?



Now having said that, it's not efficient either way because
matrices are stored column-major.  So it's better if
you take an array of vectors:

  vector[C] bla[R];

or to make it more like a matrix, row vectors:

  row_vector[C] bla[R];
Or he could just use 2d arrays as in his initial code?

real bla[R, C];  
  
real orderedbla
[R,C];  
  
for (r in 1:R) {
 
orderedbla
[r] <- sort_asc(bla[r]);  
}

(this at least parses)

Since arrays are stored in row-major order, this one will be efficient, right? It's hard to believe that it was just a matter of removing two commas.

Bob Carpenter

unread,
Aug 13, 2014, 11:20:30 AM8/13/14
to stan-...@googlegroups.com
Sure
On Aug 13, 2014, at 9:10 AM, Marco Inacio <marcoig...@gmail.com> wrote:

>
> On 14-08-13 01:08 AM, Bob Carpenter wrote:
>> You should be able to do just this:
>>
>> for (r in 1:R)
>> orderedbla[r] <- sort_asc(bla[r]);
>>
>> I worked hard to get row assignment working :-)
>>

> That's pretty cool, is assignment also possible for columns of matrices?


No --- assignment to columns isn't allowed yet.

I want to do a generalization of indexing so that we
can create lvalues (what can be assigned to) of expressions
like you'd see in R/JAGS: alpha[1:3,2,4] to take a 3D array
and return a 1D array.


> Also, this was pretty hard to find in page 20, may I suggest that we add it the description of the row function in the manual?

OK --- I added a comment in the manual issue to update:

https://github.com/stan-dev/stan/issues/786#issuecomment-52063212


>> Now having said that, it's not efficient either way because
>> matrices are stored column-major. So it's better if
>> you take an array of vectors:
>>
>> vector[C] bla[R];
>>
>> or to make it more like a matrix, row vectors:
>>
>> row_vector[C] bla[R];
>>
> Or he could just use 2d arrays as in his initial code?
>
> real bla[R, C];
>
> real orderedbla[R,C];
>
> for (r in 1:R) {
> orderedbla[r] <- sort_asc(bla[r]);
> }
>
> (this at least parses)

Yes, 2D arrays will work --- the only issue is that you can't
use their rows for vector operations.

>
> Since arrays are stored in row-major order, this one will be efficient, right? It's hard to believe that it was just a matter of removing two commas.

Either an array of vectors or an array of arrays (aka 2D array) is most efficient
in terms of access time. Matrices are a bit tighter in memory, but I doubt that
will even be measurable as it's only order o(n) overhead for an n x n matrix.

- Bob
Reply all
Reply to author
Forward
0 new messages