supermat {lazy.mat} | R Documentation |
Declare a supermatrix with SMdim attribute
supermat(nrows = 1, ncols = 1, initvar = 0)
nrows |
a vector of rows of each submatrix |
ncols |
a vector of columns of each submatrix. |
initvar |
initial value of the elements |
Note on supermatrix
A supermatrix, which sometimes is called as a partitioned matrix
or a block matrix, is a matrix whose elements are matrices.
For example, a super matrix shown below consits of 9 submatrices:
.....c1...c1...c2...c1...c2...c3...c4
r1 1111 1112 1212 1113 1213 1313 1413
r2 2111 2112 2212 2113 2213 2313 2413
r3 1121 1122 1222 1123 1223 1323 1423
r4 2121 2122 2222 2123 2223 2323 2423
r5 3121 3122 3222 3123 3223 3323 3423
r6 1131 1122 1222 1123 1233 1333 1433
r7 2131 2122 2222 2123 2233 2333 2433
r8 3131 3122 3222 3123 3233 3333 3433
r9 4131 4132 4232 4123 4233 4333 4433
Using lazy.tools and lazy.mat packages, we can create the above by
SM <- demomat(c(2,3,4),c(1,2,4),supermat=1)
.
The generated SM matrix has 9 rows and 7 colums and the SMdim attributes,
which, in this case, is a list:
list(nrows=c(2,3,4),mcols=c(1,2,4))
.
How to define supermatrix:
1) Use demomat
function and modify the submatrices later
using modSM
function.
2) Use supermatrix
function to initializes a super matrix
with the SMdim attributes and assign the submatrices later.
using modSM
function.
3) Manually define a supermatrix as a usual matrix and add the SMdim
attributes to it.
The sub matrices can be accessed by getSM
function
and can be modifed by modSM
function.
To transpose a supermatrix, use transp
function instead
since it preserves the SMdim attributes.
To print a super matrix with its submatrices being separated,
use Printb
function.
About SMdim attribute:
SMdim attribute of a supermatrix is a list of the form
(nrows=, ncols=) where
nrows is a vector consisting of the # of rows of each submatrix,
ncols is a vector consisting of the # of columns of each submatrix.
a sum(nrow) x sum(ncols) matrix with SMdim attribute: SMdim=list(nrows=nrows,ncols=ncols)
# using demomat function SM <- demomat(c(2,3,4),c(1,2,4),supermat=1) Printb(SM) getSM(SM,2,3) # transposition SMt <- transp(SM) Printb(SMt) getSM(SMt,2,3) # using surmat funcgtion and modSM function ( SM <- supermat( c(2,3,4),c(1,2,4) ) ) for( i in 1:3 ) for( j in 1:3 ) modSM(SM, i,j) <- 10*i+j Printb(SM) # using surmat funcgtion and getSM function with index option ( SM <- supermat( c(2,3),c(3,4,1) ) ) for( i in 1:2 ) for( j in 1:3 ) SM[ getSM(SM,i,j,1)$rows, getSM(SM,i,j,1)$cols ] <- 10*i+j Printb(SM) # adding the SMdim attributes later SM <- matrix( 0, sum(c(2,3)), sum(c(3,4)) ) Printb(SM) attributes(SM)$SMdim <- list(nrows=c(2,3),ncols=c(3,4)) Printb(SM) # horizontally stacking matrices Mh=" c1 c1 c2 c1 c2 c3 c4 r1 111 112 122 113 123 133 143 r2 211 212 222 213 223 233 243 r3 311 312 322 313 323 333 343 r4 411 412 422 413 423 433 443 r5 511 512 522 513 523 533 543 "; Mh=cards( Mh, header=1 ) attributes(Mh)$SMdim=list(nrows=5, ncols=c(1,2,4)) Printb(Mh, sep=" ")