Dear lists, I'm in my process of learning of writing a function. I tried to write a simple functions of a matrix and a vector. Here are the codes: mm<-function(m,n){ #matrix function w<-matrix(nrow=m, ncol=n) for(i in 1:m){ for(j in 1:n){ w[i,j]=i+j } } return(w[i,j]) } v<-function(n){ #function of a vector y=vector(length=n) for(i in 1:n){ y[i]=i } return(y[i]) } when i tried called on my matrix function; say mm(5,10). what i got is the single value of 15..where's is my matrix?? same thing happened to my vector function. Please help me figure out these problems. Thanks a bunch!! Cheers, Anisah --------------------------------- [[alternative HTML version deleted]]
Anisah, You just need to omit the indices in the return statements: mm<-function(m,n){ #matrix function w<-matrix(nrow=m, ncol=n) for(i in 1:m){ for(j in 1:n){ w[i,j]=i+j } } w } v<-function(n){ #function of a vector y=vector(length=n) for(i in 1:n){ y[i]=i } return(y) } On Fri, 2008-02-08 at 07:42 -0800, mohamed nur anisah wrote:> Dear lists, > > I'm in my process of learning of writing a function. I tried to write a simple functions of a matrix and a vector. Here are the codes: > > mm<-function(m,n){ #matrix function > w<-matrix(nrow=m, ncol=n) > for(i in 1:m){ > for(j in 1:n){ > w[i,j]=i+j > } > } > return(w[i,j]) > } > > v<-function(n){ #function of a vector > y=vector(length=n) > for(i in 1:n){ > y[i]=i > } > return(y[i]) > } > > when i tried called on my matrix function; say mm(5,10). what i got is the single value of 15..where's is my matrix?? same thing happened to my vector function. Please help me figure out these problems. Thanks a bunch!! > > Cheers, > Anisah > > > > > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Hi Mohamed, You want to return the matrix - you're returning an element of the matrix. So in your formula, insert: return(w) instead of return(w[i,j]) On Feb 8, 2008 8:42 AM, mohamed nur anisah <nuranisah_mohamed at yahoo.com> wrote:> Dear lists, > > I'm in my process of learning of writing a function. I tried to write a simple functions of a matrix and a vector. Here are the codes: > > mm<-function(m,n){ #matrix function > w<-matrix(nrow=m, ncol=n) > for(i in 1:m){ > for(j in 1:n){ > w[i,j]=i+j > } > } > return(w[i,j]) > } > > v<-function(n){ #function of a vector > y=vector(length=n) > for(i in 1:n){ > y[i]=i > } > return(y[i]) > } > > when i tried called on my matrix function; say mm(5,10). what i got is the single value of 15..where's is my matrix?? same thing happened to my vector function. Please help me figure out these problems. Thanks a bunch!! > > Cheers, > Anisah > > > > > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Matthew C Keller Asst. Professor of Psychology University of Colorado at Boulder www.matthewckeller.com
Hi Mohamed, mohamed nur anisah wrote (8.2.2008):> Dear lists, > > I'm in my process of learning of writing a function. I tried to > write a simple functions of a matrix and a vector. Here are the > codes: > > mm<-function(m,n){ #matrix function > w<-matrix(nrow=m, ncol=n) > for(i in 1:m){ > for(j in 1:n){ > w[i,j]=i+j > } > } > return(w[i,j]) > }This returns the value in row i, in column j, not the matrix. Replace 'return(w[i,k]) with just 'w'.> v<-function(n){ #function of a vector > y=vector(length=n) > for(i in 1:n){ > y[i]=i > } > return(y[i]) > }The same here: the function returns the value of the ith element in the vector. Again: Replace 'return(y[i]) with just 'y'. Please compare the outputs:> mm<-function(m,n) {+ w<-matrix(nrow=m, ncol=n) + for(i in 1:m) { + for(j in 1:n) { + w[i,j]=i+j + } + } + return(w[i,j]) + }> mm(5,10)[1] 15> mm<-function(m,n) {+ w<-matrix(nrow=m, ncol=n) + for(i in 1:m) { + for(j in 1:n) { + w[i,j]=i+j + } + } + w + }> mm(5,10)[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 2 3 4 5 6 7 8 9 10 11 [2,] 3 4 5 6 7 8 9 10 11 12 [3,] 4 5 6 7 8 9 10 11 12 13 [4,] 5 6 7 8 9 10 11 12 13 14 [5,] 6 7 8 9 10 11 12 13 14 15 Kind regards, Kimmo
Hi Mohamed, Just change return(w[i,j]) by return(w), and return(y[i]) by return(y). I hope this helps, Jorge On 2/8/08, mohamed nur anisah <nuranisah_mohamed@yahoo.com> wrote:> > Dear lists, > > I'm in my process of learning of writing a function. I tried to write a > simple functions of a matrix and a vector. Here are the codes: > > mm<-function(m,n){ #matrix function > w<-matrix(nrow=m, ncol=n) > for(i in 1:m){ > for(j in 1:n){ > w[i,j]=i+j > } > } > return(w[i,j]) > } > > v<-function(n){ #function of a vector > y=vector(length=n) > for(i in 1:n){ > y[i]=i > } > return(y[i]) > } > > when i tried called on my matrix function; say mm(5,10). what i got is the > single value of 15..where's is my matrix?? same thing happened to my vector > function. Please help me figure out these problems. Thanks a bunch!! > > Cheers, > Anisah > >[[alternative HTML version deleted]]
It is doing exactly what you ask. You are asking for the last element in the matrix w[i,j] and the last element in the vector y[i]. Try return(w) and return(y). --- mohamed nur anisah <nuranisah_mohamed at yahoo.com> wrote:> Dear lists, > > I'm in my process of learning of writing a > function. I tried to write a simple functions of a > matrix and a vector. Here are the codes: > > mm<-function(m,n){ #matrix function > > w<-matrix(nrow=m, ncol=n) > for(i in 1:m){ > for(j in 1:n){ > w[i,j]=i+j > } > } > return(w[i,j]) > } > > v<-function(n){ #function of a vector > y=vector(length=n) > for(i in 1:n){ > y[i]=i > } > return(y[i]) > } > > when i tried called on my matrix function; say > mm(5,10). what i got is the single value of > 15..where's is my matrix?? same thing happened to my > vector function. Please help me figure out these > problems. Thanks a bunch!! > > Cheers, > Anisah > > > > > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >Looking for the perfect gift? Give the gift of Flickr!
mohamed nur anisah <nuranisah_mohamed at yahoo.com> [Fri, Feb 08, 2008 at 04:42:41PM CET]:> Dear lists, > > I'm in my process of learning of writing a function. I tried to write a simple functions of a matrix and a vector. Here are the codes: > > mm<-function(m,n){ #matrix function > w<-matrix(nrow=m, ncol=n) > for(i in 1:m){ > for(j in 1:n){ > w[i,j]=i+j > } > } > return(w[i,j]) > } >In addition to the other comments, allow me to remark that R provides a lot of convenience functions on vectors that make explicit looping unnecessary. An error such as yours wouldn't have occurred to a more experienced expRt because indices wouldn't turn up in the code at all: mm <- function(m, n) { a <- array(nrow=m, ncol=n) row(a)+col(a) } Greetings Johannes -- Johannes H?sing There is something fascinating about science. One gets such wholesale returns of conjecture mailto:johannes at huesing.name from such a trifling investment of fact. http://derwisch.wikidot.com (Mark Twain, "Life on the Mississippi")