Re: [R] Stop R from changing matrix to numeric

2014-12-17 Thread Peter Alspach
Tena koe Sachin The following might help you understand what is going on and how to rectify it. cost <- function(x) {x[,1]*x[,2]} ttMat <- matrix(1:4, ncol=2) ttMat cost(ttMat) cost(ttMat[1,]) cost(as.matrix(ttMat[1,])) cost(t(as.matrix(ttMat[1,]))) cost(matrix(ttMat[1,], ncol=2)) str(ttMat) str

Re: [R] Stop R from changing matrix to numeric

2014-12-17 Thread Jeff Newmiller
as.matrix(c(1,1)) gives a matrix with only one column, but your function assumes you have at least two columns (you refer to x[,2]). Please make your examples reproducible (run it yourself in a fresh instance of R) to obtain best results with questions on this list. However, you might just be

Re: [R] Stop R from changing matrix to numeric

2014-12-16 Thread PIKAL Petr
Hi I can not reproduce your error > cost<-function(x){ + x[,1]*sin(4*x[,1])+1.1*x[,2]*sin(2*x[,2]) + } > cost(t(as.matrix(c(1,1 [1] 0.2434247 > Cheers Petr > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of > Sachinthaka Abeywardana > Sent: We