10

I need to insert a vector diagonally into a matrix on an arbitrary place. I know how to insert a vector vertically or horizontally but I can't do it diagonally.

I have:

A <- matrix(nrow=6,ncol=6)
b <- c(1:4)

The desired result (if I want to insert the vector in the position A[3,2]), would be:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   NA   NA   NA   NA   NA   NA
[2,]   NA   NA   NA   NA   NA   NA
[3,]   NA    1   NA   NA   NA   NA
[4,]   NA   NA    2   NA   NA   NA
[5,]   NA   NA   NA    3   NA   NA
[6,]   NA   NA   NA   NA    4   NA

Also, I want to be able to insert the vector to get this matrix (starting from A[4,1]):

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   NA   NA   NA    4   NA   NA
[2,]   NA   NA    3   NA   NA   NA
[3,]   NA    2   NA   NA   NA   NA
[4,]    1   NA   NA   NA   NA   NA
[5,]   NA   NA   NA   NA   NA   NA
[6,]   NA   NA   NA   NA   NA   NA
Migue
  • 1,401
  • 2
  • 15
  • 19

2 Answers2

10

Here's one possibility (you could probably wrap it up into a function)

indx <- 0:(length(b) - 1) # Create an index

Frow <- 3 ; Fcol <- 2 #initiate rows/cols
A[cbind(Frow + indx, Fcol + indx)] <- b
A
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]   NA   NA   NA   NA   NA   NA
# [2,]   NA   NA   NA   NA   NA   NA
# [3,]   NA    1   NA   NA   NA   NA
# [4,]   NA   NA    2   NA   NA   NA
# [5,]   NA   NA   NA    3   NA   NA
# [6,]   NA   NA   NA   NA    4   NA

For your second output (Assuming A matrix is all NAs again)

Frow <- 4 ; Fcol <- 1
A[cbind(Frow - indx, Fcol + indx)] <- b
A
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]   NA   NA   NA    4   NA   NA
# [2,]   NA   NA    3   NA   NA   NA
# [3,]   NA    2   NA   NA   NA   NA
# [4,]    1   NA   NA   NA   NA   NA
# [5,]   NA   NA   NA   NA   NA   NA
# [6,]   NA   NA   NA   NA   NA   NA
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • 1
    Elegant solution! Here's the according function: fill <- function(A, b, i, j, up = FALSE){ k <- seq_along(b) - 1 # create an index A[cbind(i + ifelse(up, -1, 1) * k, j + k)] <- b return(A) } fill(A, b, 3, 2) fill(A, b, 4, 1, TRUE) – Andri Signorell Feb 27 '15 at 10:01
4

You can use this function:

insert.diag <- function(A,b,start=c(1,1),dir=c(1,1)) {
  sq <- seq_along(b)-1
  indices <- sapply(1:2,function(i) start[i] + dir[i]*sq)
  stopifnot(all(indices>0))
  stopifnot(all(indices[,1]<=nrow(A)))
  stopifnot(all(indices[,2]<=ncol(A)))  
  A[indices] <- b
  A
}

Some examples of use:

A <- matrix(nrow=6,ncol=6)
b <- c(1:4)

> insert.diag(A,b,c(1,6),c(1,-1))
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   NA   NA   NA   NA   NA    1
[2,]   NA   NA   NA   NA    2   NA
[3,]   NA   NA   NA    3   NA   NA
[4,]   NA   NA    4   NA   NA   NA
[5,]   NA   NA   NA   NA   NA   NA
[6,]   NA   NA   NA   NA   NA   NA
> insert.diag(A,b,c(6,6),c(-1,-1))
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   NA   NA   NA   NA   NA   NA
[2,]   NA   NA   NA   NA   NA   NA
[3,]   NA   NA    4   NA   NA   NA
[4,]   NA   NA   NA    3   NA   NA
[5,]   NA   NA   NA   NA    2   NA
[6,]   NA   NA   NA   NA   NA    1
> insert.diag(A,b,c(1,1),c(1,1))
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1   NA   NA   NA   NA   NA
[2,]   NA    2   NA   NA   NA   NA
[3,]   NA   NA    3   NA   NA   NA
[4,]   NA   NA   NA    4   NA   NA
[5,]   NA   NA   NA   NA   NA   NA
[6,]   NA   NA   NA   NA   NA   NA
Marat Talipov
  • 13,064
  • 5
  • 34
  • 53