[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

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

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
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

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

2013-09-03 Thread PIKAL Petr
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 7 8 9 9 8 7 4 5 6 3 2 1 And I would like to have

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   63  

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
    4   10   18 2   63   64   63 3   18   10    4 4    1    3    5 A.K. From: Bert Gunter gunter.ber...@gene.com To: arun smartpink...@yahoo.com Cc: R help r-help@r-project.org Sent: Monday, September 2, 2013 10:55 AM Subject: Re: [R] Product of certain rows

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

2013-09-02 Thread arun
: 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- rbind(A,c(1,3,5)) j-seq_len(nrow(Anew)/2)###  Anew[j,]*Anew[j-1,] #Error in Anew[j, ] * Anew[j - 1, ] : non-conformable arrays t(sapply

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

2013-09-02 Thread arun
-project.org 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- rbind(A,c(1,3,5)) j-seq_len(nrow(Anew)/2)###  Anew[j,]*Anew[j-1,] #Error in Anew[j, ] * Anew[j - 1, ] : non-conformable

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

2013-09-02 Thread arun
hardy.edou...@gmail.com To: arun smartpink...@yahoo.com Cc: R help r-help@r-project.org 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

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

2013-09-02 Thread arun
...@yahoo.com To: Edouard Hardy hardy.edou...@gmail.com Cc: R help r-help@r-project.org; Bert Gunter gunter.ber...@gene.com 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.numeric(gl(nrow(Anew

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

2013-09-02 Thread Bert Gunter
gunter.ber...@gene.com 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.numeric(gl(nrow(Anew),n,nrow(Anew,function(x) apply(x,2,prod))) # V1 V2 V3 #1 252 640 1134 #2 18 30 20

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

2013-09-02 Thread arun
...@gmail.com To: arun smartpink...@yahoo.com Sent: Monday, September 2, 2013 2:32 PM Subject: Re: [R] Product of certain rows in a matrix Yes, n is 250 or more... Edouard Hardy On Mon, Sep 2, 2013 at 8:31 PM, arun smartpink...@yahoo.com wrote: Also, BTW, are you looking for n100

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

2013-09-02 Thread arun
:31 PM Subject: Re: [R] Product of certain rows in a matrix There was a slight mistake in the end: I repeated vec1.  But, it doesn't matter as the warning messages are the same. j40- n*seq_len(nrow(mat1)/n)  vec1- rep(j40,n)  res- eval(parse(text= paste(paste0(mat1,[,paste0(vec1,-,seq(n)-1

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

2013-09-02 Thread Edouard Hardy
-project.org 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- rbind(A,c(1,3,5)) j-seq_len(nrow(Anew)/2)### Anew[j,]*Anew[j-1,] #Error in Anew[j, ] * Anew[j - 1, ] : non-conformable

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

2013-09-02 Thread Edouard Hardy
: Edouard Hardy hardy.edou...@gmail.com To: arun smartpink...@yahoo.com Cc: Bert Gunter gunter.ber...@gene.com; R help r-help@r-project.org 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

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