I have v<- c(2,4,5) and mat<- matrix(0,n,n).
and i want to replace vector in the upper of the matrix, each place which i identify, the arrays are replacing as length number of vector. for example ,for n=5 the output can be :
[,1] [,2] [,3] [,4] [,5]
[1,] 0 2 4 0 0
[2,] 0 0 5 0 0
[3,] 0 0 0 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 0
I try with this code:
mat <- matrix(0, nrow = 5, ncol = 5)
for (i in 1:5){
for (j in 1:5){
if (i<j & j<= 5){
mat [upper.tri(mat, diag = FALSE)]<- v
}
}
}
but output is:
[,1] [,2] [,3] [,4] [,5]
[1,] 0 2 4 2 2
[2,] 0 0 5 4 4
[3,] 0 0 0 5 5
[4,] 0 0 0 0 2
[5,] 0 0 0 0 0
I have two problems:
I want the vector replaced just one time in matrix and not to repeat.
I don't know where I have to replace i and j in the loop for the starting point of matrix to replace vector.(maybe I want to start the vector from [3,4] in matrix.).for example:
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 0 0 0 2 4
[4,] 0 0 0 0 5
[5,] 0 0 0 0 0