Asset Classes

Free investment financial education

Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
R: Conversion between Lower Triangular Matrix and Vector

R: Conversion between Lower Triangular Matrix and Vector

Posted March 15, 2024 at 9:57 am
Sang-Heon Lee
SHLee AI Financial Model

This post shows how to convert a lower triangular matrix into a vector or vice versa using R code.

R code

The following R code efficiently converts between a lower triangular matrix and a vector, supporting both row-wise and column-wise operations for diverse purposes.

#-----------------------------------------------
# 1) Extract the lower triangular part (LTP)
#    of matrix as a vector
#-----------------------------------------------
mat <- matrix(1:9, nrow = 3)
 
# column-wise
v_cw <- mat[lower.tri(mat, diag = TRUE)] 
# row-wise
v_rw <- c(t(mat))[c(t(lower.tri(mat, diag = TRUE)))] 
 
cat("Input matrix:\n"); print(mat)
 
# vector containing the LTP
print(v_cw)
print(v_rw)
 
#-----------------------------------------------
# 2) Insert a vector into 
#    the lower triangular part of matrix
#-----------------------------------------------
A <- B <- matrix(0,3,3)
 
# column-wise
A[lower.tri(A, diag = TRUE)] <- v_cw
# row-wise
B[t(lower.tri(B, diag = TRUE))] <- v_rw; B <- t(B)
 
# reconstructed lower triangular matrix 
print(A) 
print(B) 

Output for the above code is as follows.

Input matrix:
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> 
> # vector containing the LTP
> print(v_cw)
[1] 1 2 3 5 6 9
> print(v_rw)
[1] 1 2 5 3 6 9
> 
> 
> # reconstructed lower triangular matrix 
> print(A) 
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    2    5    0
[3,]    3    6    9
> print(B) 
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    2    5    0
[3,]    3    6    9

Originally posted on SHLee AI Financial Model blog.

Join The Conversation

If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.

Leave a Reply

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from SHLee AI Financial Model and is being posted with its permission. The views expressed in this material are solely those of the author and/or SHLee AI Financial Model and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.