[R] An extention of outer() ?
Bogaso Christofer
bogaso.christofer at gmail.com
Thu Apr 7 21:29:48 CEST 2011
Hi Henrique, thanks for your help. To be frank, I really could not
understand the process going on there. I was trying with following code:
> x <- matrix(1:10, nc = 2)
> y <- 20:22
> fn1 <- function(x1, x2, y) return (x1-x2+y)
> outer(x, y, fn1, 1)
, , 1
[,1] [,2]
[1,] -18 -13
[2,] -17 -12
[3,] -16 -11
[4,] -15 -10
[5,] -14 -9
, , 2
[,1] [,2]
[1,] -19 -14
[2,] -18 -13
[3,] -17 -12
[4,] -16 -11
[5,] -15 -10
, , 3
[,1] [,2]
[1,] -20 -15
[2,] -19 -14
[3,] -18 -13
[4,] -17 -12
[5,] -16 -11
But could not really understand how I am getting those values. Would you
please help on how those numbers are coming?
Thanks and regards,
-----Original Message-----
From: Henrique Dallazuanna [mailto:wwwhsd at gmail.com]
Sent: 07 April 2011 23:36
To: Bogaso Christofer
Cc: r-help at r-project.org
Subject: Re: [R] An extention of outer() ?
Try this:
aperm(outer(x, y, fn1, y), c(3, 1, 2))[,,2]
On Thu, Apr 7, 2011 at 3:21 PM, Bogaso Christofer
<bogaso.christofer at gmail.com> wrote:
> Dear all, as per my understanding, outer() function is to facilitate
> 2-dimensional function evaluation, like to evaluate f(x,y) for
> different values of x and y. However I have slightly modified version
> of that, where x is a matrix with, say, 5 rows and 2 columns and y is
> a vector. Let take following example:
>
>
>
> fn1 <- function(x1, x2, y) return (x1+x2+y)
>
>
>
> x <- matrix(1:10, nc = 2)
>
> y <- 1:5
>
>
>
> with this input, I want to construct a 5x5 matrix whose (1,1)th
> element will be : fn1(x[1,1], x[1,2], y[1]), (1,2)th element will be :
> fn1(x[1,1], x[1,2], y[2])..... (1,5)th element will be : fn1(x[1,1],
> x[1,2], y[5]).....similarly (2,1)th element will be : fn1(x[2,1],
> x[2,2], y[1])....etc
>
>
>
> If my 'x' object is a vector then I can easily use outer() function
> effectively. However for my present case, I am looking for some
> ***outer() like function*** to get the result effectively. Can
> somebody help me on how can I do that without using any for loop?
>
>
>
> Thanks and regards,
>
>
>
>
> [[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.
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
More information about the R-help
mailing list