R: extract parts of matrices in loops - loop only run once -
i not advanced user, have been trying solve problem many hours , not know how else proceed.
setting: have 2x10000 matrix, constituted of 10000 repetitions of 2 variables, call them , b,in row 1 , 2 respectively. need operation repeated every column (i.e. 10000 times), hence loop. operation following: every column [a b]', create 2x2 matrix looking this:
[a b] [b a] then transpose matrix , multiply original column column. @ end should obtain, each original column [a b]', column 2 different values.
i build 2x10000 matrix contain result:
r <- 10000 <- matrix(rep(1, 200), nrow=2, ncol=r) gamma 2x10000 matrix columns using. apply following loop:
for (j in 1:r) { big_gamma = matrix(c(gamma[1, j], gamma[2, j], gamma[2, j], gamma[1, j]), nrow=2, ncol=2); <- solve(big_gamma)%*%gamma[, j]; } big_gamma 2x2 matrix need invert , multiply original vector. again, need done each of 10000 vectors , need vector (of equal dimension original one) output (if possible). tried different specifications, in way extract columns, only runs once (i.e. matrix changes , becomes 2x1 one)
i have looked hours on various sites , not find answer. hope question not stupid. thank in advance, soo much!
you overwriting a on each iteration. try:
r <- 10000 <- matrix(rep(1, 200), nrow=2, ncol=r) (j in 1:r) { big_gamma = matrix(c(gamma[1, j], gamma[2, j], gamma[2, j], gamma[1, j]), nrow=2, ncol=2); a[, j] <- solve(big_gamma)%*%gamma[, j]; }
Comments
Post a Comment