[R] Accessing sub diagonals / spdiag in R ?

Gabor Grothendieck ggrothendieck at gmail.com
Fri Feb 25 19:09:51 CET 2011


On Fri, Feb 25, 2011 at 11:26 AM, Mingo <catojones at gmail.com> wrote:
> Hello, I'm attempting to access a specific number of sub diagonals in a
> MATRIX and have been accustomed to using spdiags in MATLAB or Octave. I've
> got a solution pieced together using for loops and it works though isn't
> vectorized and liable to run very slow
> for large matrices.
>
> As an example:
>
> A =
> 1 2 3 4 5
> 9 8 7 6 5
> 4 5 6 7 8
> 5 4 3 2 1
> 8 7 6 0 1
>
> The subdiagonals are: 9,5,3,0   4,4,6   5,7  and 8,
> I know about lower.tri and can fetch the data in a resulting vector
> which ,in this case, would be:
>
> 9,4,5,8,5,4,7,3,6,0
>
> though I would have to manipulate this some more to extract the other
> diagonals (imagine this being done for say a 1000 x 1000 matrix). I looked
> at CRAN and didn't see anything corresponding to
> spdiags. The closest package appeared to be the one relating to sparse
> matrices and band symmetry. Would you have any suggestions about 1) how to
> emulate spdiags or 2) working with the lower.tri returned-data and
> extracting the remaining diags efficiently. I can live with what I have but
> imagine that there is something more direct. Thanks

A[ col(A) == row(A) - i ] is the ith subdiagonal

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list