Is there an efficient way to replace a sub-matrix within a matrix with another matrix? I know I can do it by looping element by element but this is not very efficient.
Code:
matrix A = J(20,4,.) // matrix of missing values
matrix B = (1,2 \ 3,4 \ 5,6 \ 7,8 \ 9,10) // smaller matrix
* and I want to do something like
matrix A[10..14,2..3] = B
* which gives
* 14 invalid name
* r(198);
* I know I can do it element by element as follows but
* this is not very efficient with big matrices
local rowcount = 1
forval row = 10/14 {
local colcount = 1
forval col = 2/3 {
matrix A[`row',`col'] = B[`rowcount',`colcount']
local ++colcount
}
local ++rowcount
}

. That being said Mata is far more powerful when it comes to work with matrices
Comment