Seb
Seb

Reputation: 5507

How to efficiently loop through a triangular matrix

I have a triangular matrix and would like to loop through all the elements efficiently. Is there a smart way that I simply don't see?

So here's a small example of my matrix

        [,1] [,2] [,3] [,4]
  [1,]    1    0    0    0
  [2,]    2    1    0    0
  [3,]    3    7    1    0
  [4,]    4    4    10   1

What I want to do is perform some function f() on the elements in this matrix that are under (over) the main diagonal. (background: I know that I have a symmetric matrix and would like to perform some time consuming data manipulations and I'd like to 'mirror' my matrix)

Upvotes: 4

Views: 2714

Answers (1)

Josh O'Brien
Josh O'Brien

Reputation: 162431

As shown below, lower.tri() and upper.tri() provide an expressive (and fast) means of extracting and replacing elements in the relevant sectors a matrix. Since the function you're applying to the elements is presumably slow compared to indexing operations, there's probably no point in searching for faster indexing options (or in trying to avoid the single call to t()).

## Example data
m <- matrix(c(1,2,3,4,0,1,7,4,0,0,1,10,0,0,0,1), ncol=4)

## Example of a slow function
slowFun <- function(x) sapply(x, function(x) {Sys.sleep(0.1); x^2})

## Proposed strategy
m[lower.tri(m)] <- slowFun(m[lower.tri(m)])
m[upper.tri(m)] <- t(m)[upper.tri(m)]      

Upvotes: 3

Related Questions