How can I make an identical matrix with a column vector? - r

I have a column vector with dimensions 4000x1, and I need to make a matrix with that vector, but the matrix needs to have the column vector as a diagonal and the other numbers as zero. Something like this:
Column Vector
> vector <- matrix(c(1:5), ncol=1, nrow=5)
> vector
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
Matrix
[,1] [,2] [,3] [,4]
a 1 0 0 0
b 0 2 0 0
c 0 0 3 0
How can I produce this output?

This sounds like the diag() function, e.g.,
> my_vect <- 1:5
> diag(my_vect)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 2 0 0 0
[3,] 0 0 3 0 0
[4,] 0 0 0 4 0
[5,] 0 0 0 0 5
By the way, as you have written it vector is actually a 5x1 matrix, so you would need to convert it to, well, a vector:
> diag(as.vector(vector))
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 2 0 0 0
[3,] 0 0 3 0 0
[4,] 0 0 0 4 0
[5,] 0 0 0 0 5

Related

How to set all rows of a list of matrices to zero using if condition statement in R

Suppose I have a matrix, mat. Suppose further that the sum of one row of this matrix is equal to zero. Then, I need to set all the coming rows (the rows after the zero row) to zero. For example,
mat <- c(1,2,0,0,0,
3,4,0,2,1,
0,0,0,1,0,
1,2,0,0,0,
0,1,0,1,0)
mat <- matrix(mat,5,5)
mat
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 0 1 0
[2,] 2 4 0 2 1
[3,] 0 0 0 0 0
[4,] 0 2 1 0 1
[5,] 0 1 0 0 0
All the entries of row 3 are zero. Hence, I want rows 4, and 5 to become zeros as well. I have a list of matrices and would like to apply the same to all the matrices using the lapply function. For simplicity, I make a list of 3 matrices similar to the mat.
mat <- c(1,2,0,0,0,
3,3,0,2,1,
0,0,0,4,0,
1,3,0,0,0,
0,1,0,1,0)
mat <- matrix(mat,5,5)
mat1 <- c(1,2,0,0,0,
3,4,0,2,1,
0,0,0,1,0,
1,2,0,0,0,
0,1,0,1,0)
mat1 <- matrix(mat1,5,5)
mat2 <- c(1,2,0,0,0,
3,4,0,2,1,
0,0,0,2,0,
1,2,0,0,0,
0,2,0,3,0)
mat2 <- matrix(mat2,5,5)
Mat <- list(mat1, mat2, mat3)
You did not actually post mat3 in your data so I just used mat3 <- matrix(1, 5, 5), i.e. a 5x5 matrix of ones. This was to ensure it could handle cases where there is no row where all values are zero.
This will return a list of matrices where all rows are zero after the first row of zeroes:
lapply(Mat, \(mat) {
first_zero_row <- which(rowSums(mat)==0)[1]
if(!is.na(first_zero_row)) {
mat[first_zero_row:nrow(mat),] <- 0
}
mat
})
Output:
[[1]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 0 1 0
[2,] 2 4 0 2 1
[3,] 0 0 0 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 0
[[2]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 0 1 0
[2,] 2 4 0 2 2
[3,] 0 0 0 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 0
[[3]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 1 1 1 1 1
[3,] 1 1 1 1 1
[4,] 1 1 1 1 1
[5,] 1 1 1 1 1
Another option could be:
lapply(Mat, function(x) {x[cumsum(rowSums(x != 0) == 0) != 0, ] <- 0; x})
[[1]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 0 1 0
[2,] 2 3 0 3 1
[3,] 0 0 0 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 0
[[2]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 0 1 0
[2,] 2 4 0 2 1
[3,] 0 0 0 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 0
[[3]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 0 1 0
[2,] 2 4 0 2 2
[3,] 0 0 0 1 0
[4,] 0 2 2 0 3
[5,] 0 1 0 0 0

replace a vector to an upper Triangle matrix with different length

I want to have a triangle matrix by a vector when length of vector is less than replacement length.
for example:
v<- c(1,2,3,4,5,6)
and
mat<- matrix(0,5,5).
If I use
mat[upper.tri(mat, diag=FALSE)]<- v
,the result is:
[,1] [,2] [,3] [,4] [,5]
[1,] 0 1 2 4 1
[2,] 0 0 3 5 2
[3,] 0 0 0 6 3
[4,] 0 0 0 0 4
[5,] 0 0 0 0 0
But i don't want to replace more than length of vector in matrix. And i want to have:
[1,] 0 1 2 4 0
[2,] 0 0 3 5 0
[3,] 0 0 0 6 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 0
You could adjust the length of v to that of the upper triangle. This yields some NA values that you can replace with zeroes.
u.tri <- upper.tri(mat, diag=FALSE)
mat[u.tri] <- `length<-`(v, length(u.tri))
mat[is.na(mat)] <- 0
# [,1] [,2] [,3] [,4] [,5]
# [1,] 0 1 2 4 0
# [2,] 0 0 3 5 0
# [3,] 0 0 0 6 0
# [4,] 0 0 0 0 0
# [5,] 0 0 0 0 0

Change multiple Matrix elements by Index Vectors

I have a matrix
myMatrix <- matrix(data = 0, nrow = 4, ncol = 4)
[,1] [,2] [,3] [,4]
[1,] 0 0 0 0
[2,] 0 0 0 0
[3,] 0 0 0 0
[4,] 0 0 0 0
and I want to change particular values
myMatrix[1,1] <- 1
myMatrix[2,3] <- 1
myMatrix[4,4] <- 1
myMatrix
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 0 1 0
[3,] 0 0 0 0
[4,] 0 0 0 1
How can I do this efficient/elegantly if I have two vectors containing the row and column indexes:
rowIndexes <- c(1,2,4)
colIndexes <- c(1,3,4)
The assigned value is constant (in this case 1).
I know how to do it with a for-loop, but this feels inefficient.
We can cbind the row/column index, subset the myMatrix and assign values to 1
myMatrix[cbind(rowIndexes, colIndexes)] <- 1
myMatrix
# [,1] [,2] [,3] [,4]
#[1,] 1 0 0 0
#[2,] 0 0 1 0
#[3,] 0 0 0 0
#[4,] 0 0 0 1

Lapply function does not work for my matrices

The problem:
I have a list of matrices. I would like to convert all the rows, unless the last row, to zero. I tried the lapply function but it does not work as expected.
Example:
x <- matrix(3,4,4)
y <- matrix(5,4,5)
z <- list(x,y)
NewZ <- lapply(1:2, function(i) z[[i]][-nrow(z[[i]]), ] <- 0)
The lapply returns me this:
> NewZ
[[1]]
[1] 0
[[2]]
[1] 0
I would like to get matrices like this:
> z[[1]][-nrow(z[[1]]),] <- 0
> z[[1]]
[,1] [,2] [,3] [,4]
[1,] 0 0 0 0
[2,] 0 0 0 0
[3,] 0 0 0 0
[4,] 3 3 3 3
> z[[2]][-nrow(z[[2]]),] <- 0
> z[[2]]
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 0 0 0 0 0
[4,] 5 5 5 5 5
Where is my mistake? any idea, please?
You are using lapply() like a for loop. lapply loops through the list on its own, no need to use indexing:
NewZ <- lapply(z, function(x){
x[-nrow(x),] <- 0
return(x)
})
> NewZ
[[1]]
[,1] [,2] [,3] [,4]
[1,] 0 0 0 0
[2,] 0 0 0 0
[3,] 0 0 0 0
[4,] 3 3 3 3
[[2]]
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 0 0 0 0 0
[4,] 5 5 5 5 5
Also, when you change specific parts of a list element with lapply (meaning you are using the <- operator within the call), you need to return the element.

use of the [<- operator to modify a line of data

I have some data
data <- diag(5)
I want to use the [<- operator to change a line.
The result should be:
data[1,] <- 2
> data
[,1] [,2] [,3] [,4] [,5]
[1,] 2 2 2 2 2
[2,] 0 1 0 0 0
[3,] 0 0 1 0 0
[4,] 0 0 0 1 0
[5,] 0 0 0 0 1
I know I can do e.g.
`[<-`(data, i=1, j=3, 2)
which gives
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 8 0 0
[2,] 0 1 0 0 0
[3,] 0 0 1 0 0
[4,] 0 0 0 1 0
[5,] 0 0 0 0 1
but how can I operate on line (or column, same issue)?
I tried j=NULL, j=integer(0), it doesn't work. I could do j=1:5 and get what I want but I am wondering how to mimic data[1,] <- 2 and not data[1,1:5] <- 2.
> `[<-`(data, 1, , 2) # blank 2nd argument
[,1] [,2] [,3] [,4] [,5]
[1,] 2 2 2 2 2
[2,] 0 1 0 0 0
[3,] 0 0 1 0 0
[4,] 0 0 0 1 0
[5,] 0 0 0 0 1
You can use ncol to ensure that all columns are set:
`[<-`(data, i = 1, j = 1:ncol(data), 2)

Resources