I have the following cost function:
cost<-function(x){
x[,1]*sin(4*x[,1])+1.1*x[,2]*sin(2*x[,2])
}
If I send in a matrix which has MORE than one row and 2 columns, this
works fine. However, if I try to do cost(t(as.matrix(c(1,1)))) it
gives me an index error. When I tried debugging it, I found that the
type of matrix 'x' was converted to numeric.
How do I prevent this conversion from matrix to numeric if its a
matrix with just one row? The only way it would work is if I have:
x[1]*sin(4*x[1])+1.1*x[2]*sin(2*x[2]), which in turn wont work with a
matrix input.
Thanks,
Sachin
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-bounces at r-project.org] On Behalf Of > Sachinthaka Abeywardana > Sent: Wednesday, December 17, 2014 7:55 AM > To: r-help at r-project.org > Subject: [R] Stop R from changing matrix to numeric > > I have the following cost function: > cost<-function(x){ > x[,1]*sin(4*x[,1])+1.1*x[,2]*sin(2*x[,2]) > } > > If I send in a matrix which has MORE than one row and 2 columns, this > works fine. However, if I try to do cost(t(as.matrix(c(1,1)))) it gives > me an index error. When I tried debugging it, I found that the type of > matrix 'x' was converted to numeric. > > How do I prevent this conversion from matrix to numeric if its a matrix > with just one row? The only way it would work is if I have: > x[1]*sin(4*x[1])+1.1*x[2]*sin(2*x[2]), which in turn wont work with a > matrix input. > > Thanks, > Sachin > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
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 needing to read about the drop parameter for
indexing:
?"["
which is also mentioned in the Introduction to R document that accompanies the
software in the section on indexing.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On December 16, 2014 10:55:27 PM PST, Sachinthaka Abeywardana
<sachin.abeywardana at gmail.com> wrote:>I have the following cost function:
>cost<-function(x){
> x[,1]*sin(4*x[,1])+1.1*x[,2]*sin(2*x[,2])
>}
>
>If I send in a matrix which has MORE than one row and 2 columns, this
>works fine. However, if I try to do cost(t(as.matrix(c(1,1)))) it
>gives me an index error. When I tried debugging it, I found that the
>type of matrix 'x' was converted to numeric.
>
>How do I prevent this conversion from matrix to numeric if its a
>matrix with just one row? The only way it would work is if I have:
>x[1]*sin(4*x[1])+1.1*x[2]*sin(2*x[2]), which in turn wont work with a
>matrix input.
>
>Thanks,
>Sachin
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
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(ttMat[1,])
str(as.matrix(ttMat[1,]))
str(t(as.matrix(ttMat[1,])))
str(matrix(ttMat[1,], ncol=2))
HTH .....
Peter Alspach
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Sachinthaka
Abeywardana
Sent: Wednesday, 17 December 2014 7:55 p.m.
To: r-help at r-project.org
Subject: [R] Stop R from changing matrix to numeric
I have the following cost function:
cost<-function(x){
x[,1]*sin(4*x[,1])+1.1*x[,2]*sin(2*x[,2])
}
If I send in a matrix which has MORE than one row and 2 columns, this works
fine. However, if I try to do cost(t(as.matrix(c(1,1)))) it gives me an index
error. When I tried debugging it, I found that the type of matrix 'x'
was converted to numeric.
How do I prevent this conversion from matrix to numeric if its a matrix with
just one row? The only way it would work is if I have:
x[1]*sin(4*x[1])+1.1*x[2]*sin(2*x[2]), which in turn wont work with a matrix
input.
Thanks,
Sachin
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
The contents of this e-mail are confidential and may be ...{{dropped:14}}