2

Given a matrix A of dimension n x n, my objective is to get all submatrices A[1:mid, (mid+1):n] and convert them as a list of vectors. Here mid runs from 1 to n-1.

For example, if A = matrix(1:16, 4, 4), then the result will be res = list(as.vector(A[1:1,2:4]), as.vector(A[1:2,3:4]), as.vector(A[1:3,4:4])). A for loop can achieve this but it takes lots of time when n is huge.

yliueagle
  • 1,191
  • 1
  • 7
  • 22

1 Answers1

1

Create a function to do it within an apply:

solve=function(n,x){
  c(x[1:n,(n+1):dim(x)[2]])
}
sapply(1:3,solve,x=A)