loops - R error on matrix multiplication: non-conformable arguments -


i have r loop has been giving me error. here dimensions of matrices..

      > dim(a)      [1] 2 2      > dim(backward)      [1] 6848    2 

i trying run loop , following error:

      (i in t:1){       backward[i,]=a%*%t(backward[i,])}       error in %*% t(backward[i, ]) : non-conformable arguments 

where t equals 6848. time.

edit bgoldst code:

           >           [,1] [,2]         [1,]  0.8  0.2         [2,]  0.2  0.8         > backward <- matrix(1:(t*2),t,2);         >   dim(backward)        [1] 6848    2        > (i in t:1) backward[i,] <- a%*%t(backward[i,,drop=f]);        error in %*% t(backward[i, , drop = f]) : non-conformable arguments 

i'm guessing expectation of

backward[i,] 

is return 1x2 matrix, able use operand of matrix multiplication. incorrect. in r, when specify single index within dimension of matrix, default, r "drop" dimension. in case of above piece of code, row dimension dropped, , end vector, contents taken columns along indexed row. vector not valid operand matrix multiplication.

you can solve problem providing drop argument [ operation:

a <- matrix(1:(2*2),2,2); backward <- matrix(1:(6848*2),6848,2); t <- nrow(backward); (i in t:1) backward[i,] <- a%*%t(backward[i,,drop=f]); ## no error 

here's demo of effect of drop=f:

backward[1,] ## [1] 20548 27398 backward[1,,drop=f] ##       [,1]  [,2] ## [1,] 20548 27398 

see ?`[` more info.


here's solution doesn't depend on drop=f argument:

for (i in t:1) backward[i,] <- a%*%t(matrix(backward[i,],1)); 

Comments

Popular posts from this blog

How to group boxplot outliers in gnuplot -

cakephp - simple blog with croogo -

bash - Performing variable substitution in a string -