Say I have the following matrix mat, which is a binary indicator matrix:
mat<-matrix(c(1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1), byrow=T, nrow=3)
> mat
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 0 0 0 0
[2,] 0 0 1 1 0 0
[3,] 0 0 0 0 1 1
This matrix has only 3 rows. I need to create one with 10000 rows, with the same pattern of pairs of 1s on the diagonals. E.g. for 5 rows, I expect a 5 x 10 matrix:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1 0 0 0 0 0 0 0 0
[2,] 0 0 1 1 0 0 0 0 0 0
[3,] 0 0 0 0 1 1 0 0 0 0
[4,] 0 0 0 0 0 0 1 1 0 0
[5,] 0 0 0 0 0 0 0 0 1 1
Does anyone know a simple way to do that? Thanks a lot
D = diag( v ) returns a square diagonal matrix with the elements of vector v on the main diagonal. D = diag( v , k ) places the elements of vector v on the k th diagonal. k=0 represents the main diagonal, k>0 is above the main diagonal, and k<0 is below the main diagonal.
A square matrix in which every element except the principal diagonal elements is zero is called a Diagonal Matrix. A square matrix D = [dij]n x n will be called a diagonal matrix if dij = 0, whenever i is not equal to j. There are many types of matrices like the Identity matrix.
The inverse of a diagonal matrix is a diagonal matrix where the elements of the main diagonal are the reciprocals of the corresponding elements of the original diagonal matrix.
A Hankel matrix. A square diagonal matrix, with all entries on the main diagonal equal to 1, and the rest 0.
This is a sparse matrix, and, as such, you'll do much better referencing the non-zero entries: this will save you RAM and make it easier to automatically generate the matrix.
Each entry is indexed as (i,j,x), referring to the row, column, and value. Suppose that you have N (say N = 10) rows that you'd like to fill, then you are producing 2 entries per row (indexed by i
, in the code below); each column is used only once, so there are 2*N unique column values. Each non-zero entry is 1.
The code for producing this is:
N = 10
i = rep(1:N, each = 2)
j = 1:(2*N)
v = 1
library(Matrix)
mat = sparseMatrix(i = i, j = j, x = v)
The resulting matrix is:
> mat
10 x 20 sparse Matrix of class "dgCMatrix"
[1,] 1 1 . . . . . . . . . . . . . . . . . .
[2,] . . 1 1 . . . . . . . . . . . . . . . .
[3,] . . . . 1 1 . . . . . . . . . . . . . .
[4,] . . . . . . 1 1 . . . . . . . . . . . .
[5,] . . . . . . . . 1 1 . . . . . . . . . .
[6,] . . . . . . . . . . 1 1 . . . . . . . .
[7,] . . . . . . . . . . . . 1 1 . . . . . .
[8,] . . . . . . . . . . . . . . 1 1 . . . .
[9,] . . . . . . . . . . . . . . . . 1 1 . .
[10,] . . . . . . . . . . . . . . . . . . 1 1
Just use the code above and set N = 10000, and you'll have your matrix.
As an added bonus: your desired matrix (N = 1E5) consumes only 321424 bytes. In contrast, a standard dense matrix of size 10K x 20K will take 1.6GB, using numeric (i.e. 8 byte) entries. As they said in "Contact": that seems like an awful waste of space, right?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With