Re: [R] Product of certain rows in a matrix without loop

2013-09-03 Thread PIKAL Petr
9:34 AM > To: R help > Subject: [R] Product of certain rows in a matrix without loop > > Hello everybody. > Thank you again to Bert and Arun for their help on my previous > question. > I know have the following problem: > I have a matrix : > A = > 1 2 3 > 4 5 6

Re: [R] Product of certain rows in a matrix without loop

2013-09-03 Thread Gerrit Eichner
Ok, here is a bandmatrix solution "by hand": leftmatrix <- matrix( c( rep( 1, k), rep( 0, nrow(A) - k + 1)), byrow = TRUE, ncol = nrow(A), nrow = nrow(A) - k + 1) Gerrit Thank you very much for your answer. Unfortunately, I cannot use any package... Er, ... this is qui

Re: [R] Product of certain rows in a matrix without loop

2013-09-03 Thread Gerrit Eichner
Thank you very much for your answer. Unfortunately, I cannot use any package... Er, ... this is quite unusual! (Is this is homework?) Do you have a solution ? Well, take a look at the resulting bandmatrix leftmatrix. Yould can certainly build it yourself "by hand" somehow. I used the Matrix pa

Re: [R] Product of certain rows in a matrix without loop

2013-09-03 Thread Edouard Hardy
Thank you very much for your answer. Unfortunately, I cannot use any package... Do you have a solution ? Thank you in advance Edouard Hardy On Tue, Sep 3, 2013 at 11:49 AM, Gerrit Eichner < gerrit.eich...@math.uni-giessen.de> wrote: > Hello, Edouard, > > taking logs of A's elements (so that *

Re: [R] Product of certain rows in a matrix without loop

2013-09-03 Thread Gerrit Eichner
Hello, Edouard, taking logs of A's elements (so that * turns into +, so to say), using a left-multiplication with a certain band matrix of the package Matrix, and exponentiating the result again could provide a solution (see below). I know have the following problem: I have a matrix : A = 1

[R] Product of certain rows in a matrix without loop

2013-09-03 Thread Edouard Hardy
Hello everybody. Thank you again to Bert and Arun for their help on my previous question. I know have the following problem: I have a matrix : A = 1 2 3 4 5 6 7 8 9 9 8 7 4 5 6 3 2 1 And I would like to have : B = 1*4*7 2*5*8 3*6*9 4*7*9 5*8*8 6*9*7 7*9*4 8*8*5 9*7*6 9*4*3 8*5*2

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread Bert Gunter
Gents: The "eval(parse(...))" construction should almost always be avoided: it is basically a misuse of R. There are exceptions, I suppose, but this does not appear to be one of them. Note that the use of numeric indexing does appear to be slightly faster than logical indexing here, although I wo

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread Edouard Hardy
18 30 20 > > A.K. > > From: Edouard Hardy > To: arun > Cc: Bert Gunter ; R help > Sent: Monday, September 2, 2013 11:46 AM > Subject: Re: [R] Product of certain rows in a matrix > > > > Thank you all for your responses. >

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread Edouard Hardy
- Original Message - > From: arun > To: Bert Gunter > Cc: R help > Sent: Monday, September 2, 2013 11:26 AM > Subject: Re: [R] Product of certain rows in a matrix > > Hi Bert, > Thanks. It is a better solution. > > If nrow() is not even. > > Anew<- r

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread arun
- 0, ] * mat1[j40 - 1, ] * mat1[j40 - 2, ] * mat1[j40 -  :   NAs produced by integer overflow 5: In mat1[j40 - 0, ] * mat1[j40 - 1, ] * mat1[j40 - 2, ] * mat1[j40 -  :   NAs produced by integer overflow 6: In mat1[j40 - 0, ] * mat1[j40 - 1, ] * mat1[j40 - 2, ] * mat1[j40 -  : - Original

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread arun
er overflow 4: In mat1[j22 - 0, ] * mat1[j22 - 1, ] * mat1[j22 - 2, ] * mat1[j22 -  :  head(res,3) # [,1] [,2] [,3] #[1,]   NA   NA   NA #[2,]   NA   NA   NA #[3,]   NA   NA   NA A.K. From: Edouard Hardy To: arun Sent: Monday, September 2, 2013 2:32 PM Subject: Re: [R] Product of certa

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread Bert Gunter
; #user system elapsed > # 5.576 0.000 5.583 > dimnames(res6Alt)<- dimnames(res6New) > > > all.equal(res6New,res6Alt) > #[1] TRUE > > > A.K. > > > > As you said, this is very lng. > Do you have a better solution on big data ? > > > >

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread arun
ng. Do you have a better solution on big data ? - Original Message - From: arun To: Edouard Hardy Cc: R help ; Bert Gunter Sent: Monday, September 2, 2013 12:07 PM Subject: Re: [R] Product of certain rows in a matrix Hi, No problem. n<- 4 t(sapply(split(as.data.frame(Anew),as.nume

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread arun
rdy To: arun Cc: R help Sent: Monday, September 2, 2013 11:58 AM Subject: Re: [R] Product of certain rows in a matrix Thank you A.K. And do you have a solution without installing any package ? Thank you in advance. E.H. Edouard Hardy On Mon, Sep 2, 2013 at 5:56 PM, arun wrote: > &g

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread arun
#  [,1] [,2] [,3] #1  252  640 1134 #2   18   30   20 A.K. From: Edouard Hardy To: arun Cc: Bert Gunter ; R help Sent: Monday, September 2, 2013 11:46 AM Subject: Re: [R] Product of certain rows in a matrix Thank you all for your responses. The real problem i

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread arun
umeric(gl(nrow(Anew),2,7))),colProds))   [,1] [,2] [,3] 1    4   10   18 2   63   64   63 3   18   10    4 4    1    3    5 A.K. From: Bert Gunter To: arun Cc: R help Sent: Monday, September 2, 2013 10:55 AM Subject: Re: [R] Product of certain rows in a m

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread arun
2] [,3] 1    4   10   18 2   63   64   63 3   18   10    4 4    1    3    5 A.K. From: Bert Gunter To: arun Cc: R help Sent: Monday, September 2, 2013 10:55 AM Subject: Re: [R] Product of certain rows in a matrix These elaborate manipulations are unnec

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread Bert Gunter
These elaborate manipulations are unnecessary and inefficient. Use indexing instead: j <- 2*seq_len(nrow(A)/2) b <- A[j,]*A[j-1,] b [,1] [,2] [,3] [1,]4 10 18 [2,] 63 64 63 [3,] 18 104 [,1] [,2] [,3] [1,]4 10 18 [2,] 63 64 63 [3,] 18 104 [,1

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread arun
Hi, You could try: A<- matrix(unlist(read.table(text=" 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 ",sep="",header=FALSE)),ncol=3,byrow=FALSE,dimnames=NULL) library(matrixStats)  res1<-t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),colProds))  res1 #  [,1] [,2] [,3] #1    4   10   18 #2   6