LU factorization

I have created a random n*n matrix by:

i<-round(runif(100,1,100),digits=0)
n<-3
datak<-i
elasticity<-matrix(datak,byrow = TRUE,nrow=n,ncol = n)

Now , I want to have Lower triangle and Upper Triangle.
When using "upper.tri" , I am getting values in term True and False. I am not getting the values. And when I tried 'as.numeric' it gave me values as 1 and 0's.

Something like this?

set.seed(245183)
m = matrix(data = sample(1:100), nrow = 10, ncol = 10)
m[upper.tri(x = m, diag = TRUE)] = NA
print(m)

Yielding:

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
 [2,]   44   NA   NA   NA   NA   NA   NA   NA   NA    NA
 [3,]   37   26   NA   NA   NA   NA   NA   NA   NA    NA
 [4,]    1   48   84   NA   NA   NA   NA   NA   NA    NA
 [5,]   50   67   66   85   NA   NA   NA   NA   NA    NA
 [6,]   27   77   42   30   31   NA   NA   NA   NA    NA
 [7,]   79    4   73   16   29  100   NA   NA   NA    NA
 [8,]   62   87   97   32   89   88   70   NA   NA    NA
 [9,]   69   76   25   54   14   49   47   21   NA    NA
[10,]    5   52    3   86   75    2   60   64   61    NA

Hope it helps :slightly_smiling_face:

2 Likes

First, what is the value '245183' in set.seed?
Second, in Upper triangle, [1,1] should be same as before and values below column 1 except that should be zero. Similarly, [1,2] and [2,2] should be same as before and values below that should be zero. And so on.
Eg.,
u11 u12 u13
0 u22 u23
0 0 u33

In short, it ensures reproducible (pseudo) random number generation. I.e. so when you repeat my code, you will get the same numbers in the matrix. (For more, see here)

Then you do like this:

set.seed(245183)
m = matrix(data = sample(1:100), nrow = 10, ncol = 10)
print(m)
m[lower.tri(x = m, diag = FALSE)] = 0
print(m)

Yielding

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]   18   57   15   96   11   10   82   72    9    12
 [2,]   44   99   20   83   65   59   58   33   34    71
 [3,]   37   26   94   19   55   23   98   68   95    22
 [4,]    1   48   84   74    6   93   41   45   36    17
 [5,]   50   67   66   85   56   90   51   78   91    63
 [6,]   27   77   42   30   31   80   81   28   92    40
 [7,]   79    4   73   16   29  100   39   13   43    35
 [8,]   62   87   97   32   89   88   70   24   46     8
 [9,]   69   76   25   54   14   49   47   21   38    53
[10,]    5   52    3   86   75    2   60   64   61     7
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]   18   57   15   96   11   10   82   72    9    12
 [2,]    0   99   20   83   65   59   58   33   34    71
 [3,]    0    0   94   19   55   23   98   68   95    22
 [4,]    0    0    0   74    6   93   41   45   36    17
 [5,]    0    0    0    0   56   90   51   78   91    63
 [6,]    0    0    0    0    0   80   81   28   92    40
 [7,]    0    0    0    0    0    0   39   13   43    35
 [8,]    0    0    0    0    0    0    0   24   46     8
 [9,]    0    0    0    0    0    0    0    0   38    53
[10,]    0    0    0    0    0    0    0    0    0     7

Make sure you understand what the line m[lower.tri(x = m, diag = FALSE)] = 0 does, i.e. in the m-matrix, select all elements in the lower triangle excluding the diagonal and replace them with the value 0

Hope it helps :slightly_smiling_face:

1 Like

got it. thank you for helping out.

I want to obtain the same result this time, but without using formulas like upper.tri and lower.tri.
I know the code without this would be long but I want to know it to understand the whole thing.

You can take a look at this then :slightly_smiling_face:

set.seed(245183)
m = matrix(data = sample(1:100), nrow = 10, ncol = 10)
print(m)
# Loop thorugh i'th row and j'th column of m
for( i in 1:nrow(m) ){
  for( j in 1:ncol(m) ){
    # This doesn't do anything, but here you can think about how to check
    # where in the matrix you are by checking the relative values of i and j
    m[i,j] = m[i,j]
  }
}