This function returns the (rc×rc) commutation matrix P such that P\mboxvec(Y)=\mboxvec(Y′),
where Y is a (r×c) matrix (Magnus and Neudecker, 2019).
commat(r, c)
Arguments
r: Number of rows of Y.
c: Number of columns of Y.
Returns
A sparse (rc×rc) matrix, P.
Examples
Y <- matrix(rnorm(30),5,6)P <- commat(5,6)P %*% as.vector(Y)== as.vector(t(Y))# check
References
Magnus, J.R. and Neudecker, H. (2019), Matrix Differential Calculus with Applications in Statistics and Econometrics, third edition, New York, Wiley, pp. 54-55.