Quantcast

Outer product from matrix by row and a vector

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Outer product from matrix by row and a vector

Ingo Reinhold
Dear all,

I am facing a challenge when applying the outer product with a matrix by rows.

What I have is a rather big matrix, which I would like to convert into a different matrix by doing something like

outer(matrix_row, vector, function(x,y) x[1]+5*x[4]/y)

In order to get there, I tried to get the matrix into a list using the mefa package, as subsetting the original data appears too complicated. However, if I apply the following code I always get a dimensional error, as the unlist() function is applied to the whole list.

library(mefa)
a<-matrix(c(1:6),ncol=3)
b<-c(1,2,3,4)
a<-mat2list(a,MARGIN=1)

outer(a,b,function(x,y){unlist(a)[1]+unlist(a)[3]/b})


Any idea how to do this more elegantly than a nested for-loop?

Many thanks,

Ingo

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Outer product from matrix by row and a vector

David Winsemius

On Aug 7, 2012, at 9:02 PM, Ingo Reinhold wrote:

> Dear all,
>
> I am facing a challenge when applying the outer product with a  
> matrix by rows.
>
> What I have is a rather big matrix, which I would like to convert  
> into a different matrix by doing something like
>
> outer(matrix_row, vector, function(x,y) x[1]+5*x[4]/y)


>
> In order to get there, I tried to get the matrix into a list using  
> the mefa package, as subsetting the original data appears too  
> complicated. However, if I apply the following code I always get a  
> dimensional error, as the unlist() function is applied to the whole  
> list.
>
> library(mefa)
> a<-matrix(c(1:6),ncol=3)
> b<-c(1,2,3,4)
> a<-mat2list(a,MARGIN=1)
>
> outer(a,b,function(x,y){unlist(a)[1]+unlist(a)[3]/b})

(You really should not be referring to a and b inside that function  
body. It's arguments are now named x and y.)

Can you tell us what the answer _should_ be for the first entry? And a  
step by step illustration of how you got there?

If this is not what it should be, then you need to read up on the  
expected result of the division of a scalar by a vector in R.

  apply(a, 1, function(x) { x[1] + x[3]/ b } )

          [,1] [,2]
[1,] 6.000000  8.0
[2,] 3.500000  5.0
[3,] 2.666667  4.0
[4,] 2.250000  3.5

>
>
> Any idea how to do this

Do "this" is still somewhat nebulous.

> more elegantly than a nested for-loop?
>
> Many thanks,
>
> Ingo
>

David Winsemius, MD
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Outer product from matrix by row and a vector

Jeff Newmiller
In reply to this post by Ingo Reinhold
Can you post what you want your answer to be for the a and b you have given already?
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        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.

Ingo Reinhold <[hidden email]> wrote:

>Dear all,
>
>I am facing a challenge when applying the outer product with a matrix
>by rows.
>
>What I have is a rather big matrix, which I would like to convert into
>a different matrix by doing something like
>
>outer(matrix_row, vector, function(x,y) x[1]+5*x[4]/y)
>
>In order to get there, I tried to get the matrix into a list using the
>mefa package, as subsetting the original data appears too complicated.
>However, if I apply the following code I always get a dimensional
>error, as the unlist() function is applied to the whole list.
>
>library(mefa)
>a<-matrix(c(1:6),ncol=3)
>b<-c(1,2,3,4)
>a<-mat2list(a,MARGIN=1)
>
>outer(a,b,function(x,y){unlist(a)[1]+unlist(a)[3]/b})
>
>
>Any idea how to do this more elegantly than a nested for-loop?
>
>Many thanks,
>
>Ingo
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Outer product from matrix by row and a vector

Ingo Reinhold
Hi Jeff, David,

what I'm trying to do is speed the currently used nest for loop

a<-matrix(c(1:6),ncol=3)
b<-c(1,2,3,4)

result<-matrix(rep(0, times=dim(a)[1]*length(b)),nrow=dim(a)[1])
for(ii in 1:dim(a)[1]){
  for(jj in 1:length(b)){
    result[ii,jj]<-a[ii,1]+a[ii,3]/b[jj]
      }
}

giving the result

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

Thanks for taking a look.

Cheers,

Ingo

________________________________________
From: Jeff Newmiller [[hidden email]]
Sent: Wednesday, August 08, 2012 6:40 AM
To: Ingo Reinhold; [hidden email]
Subject: Re: [R] Outer product from matrix by row and a vector

Can you post what you want your answer to be for the a and b you have given already?
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        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.

Ingo Reinhold <[hidden email]> wrote:

>Dear all,
>
>I am facing a challenge when applying the outer product with a matrix
>by rows.
>
>What I have is a rather big matrix, which I would like to convert into
>a different matrix by doing something like
>
>outer(matrix_row, vector, function(x,y) x[1]+5*x[4]/y)
>
>In order to get there, I tried to get the matrix into a list using the
>mefa package, as subsetting the original data appears too complicated.
>However, if I apply the following code I always get a dimensional
>error, as the unlist() function is applied to the whole list.
>
>library(mefa)
>a<-matrix(c(1:6),ncol=3)
>b<-c(1,2,3,4)
>a<-mat2list(a,MARGIN=1)
>
>outer(a,b,function(x,y){unlist(a)[1]+unlist(a)[3]/b})
>
>
>Any idea how to do this more elegantly than a nested for-loop?
>
>Many thanks,
>
>Ingo
>
>       [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Outer product from matrix by row and a vector

Petr Savicky
On Wed, Aug 08, 2012 at 06:03:19AM +0000, Ingo Reinhold wrote:

> Hi Jeff, David,
>
> what I'm trying to do is speed the currently used nest for loop
>
> a<-matrix(c(1:6),ncol=3)
> b<-c(1,2,3,4)
>
> result<-matrix(rep(0, times=dim(a)[1]*length(b)),nrow=dim(a)[1])
> for(ii in 1:dim(a)[1]){
>   for(jj in 1:length(b)){
>     result[ii,jj]<-a[ii,1]+a[ii,3]/b[jj]
>       }
> }
>
> giving the result
>
>      [,1] [,2] [,3]
> [1,]    1    3    5
> [2,]    2    4    6

Hi.

The printed matrix is "a". The above code yields on my computer

       [,1] [,2]     [,3] [,4]
  [1,]    6  3.5 2.666667 2.25
  [2,]    8  5.0 4.000000 3.50

Try the following

  out <- a[, 1] + a[, 3] %o% (1/b)

  max(abs(out - result))

  [1] 4.440892e-16

Hope this helps.

Petr Savicky.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Outer product from matrix by row and a vector

David Winsemius

On Aug 7, 2012, at 11:26 PM, Petr Savicky wrote:

> On Wed, Aug 08, 2012 at 06:03:19AM +0000, Ingo Reinhold wrote:
>> Hi Jeff, David,
>>
>> what I'm trying to do is speed the currently used nest for loop
>>
>> a<-matrix(c(1:6),ncol=3)
>> b<-c(1,2,3,4)
>>
>> result<-matrix(rep(0, times=dim(a)[1]*length(b)),nrow=dim(a)[1])
>> for(ii in 1:dim(a)[1]){
>>  for(jj in 1:length(b)){
>>    result[ii,jj]<-a[ii,1]+a[ii,3]/b[jj]
>>      }
>> }
>>
>> giving the result
>>
>>     [,1] [,2] [,3]
>> [1,]    1    3    5
>> [2,]    2    4    6

That is not what result equals (as Petr illustrates.)

>
> Hi.
>
> The printed matrix is "a". The above code yields on my computer
>
>       [,1] [,2]     [,3] [,4]
>  [1,]    6  3.5 2.666667 2.25
>  [2,]    8  5.0 4.000000 3.50
>
> Try the following
>
>  out <- a[, 1] + a[, 3] %o% (1/b)
>
>  max(abs(out - result))
>
>  [1] 4.440892e-16
>
> Hope this helps.

It's also the case that the printed result is just the transpose of  
what I earlier offered as a possible implementation of the OP's pseudo-
code.:

 > identical( t( apply(a, 1, function(x) { x[1] + x[3]/ b } ) ), result)
[1] TRUE

--

David Winsemius, MD
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Outer product from matrix by row and a vector

Ingo Reinhold
In reply to this post by Ingo Reinhold
Hi David, Petr,

sorry for the fuzzy posts. I oversaw that it was just the transposed version as you indicate. Does exactly what it should.

Many thanks,

Ingo
______________________________________________
[hidden email] 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.
Loading...