[R] On matrix calculation

2013-04-17 Thread Christofer Bogaso
Hello again, Let say I have a matrix: Mat - matrix(1:12, 4, 3) And a vector: Vec - 5:8 Now I want to do following: Each element of row-i in 'Mat' will be divided by i-th element of Vec Is there any direct way to doing that? Thanks for your help

Re: [R] On matrix calculation

2013-04-17 Thread arun
    1 1.80 #[2,] 0.333    1 1.67 #[3,] 0.4285714    1 1.571429 #[4,] 0.500    1 1.50 A.K. - Original Message - From: Christofer Bogaso bogaso.christo...@gmail.com To: r-help r-help@r-project.org Cc: Sent: Wednesday, April 17, 2013 8:39 AM Subject: [R] On matrix

Re: [R] On matrix calculation

2013-04-17 Thread Berend Hasselman
On 17-04-2013, at 14:39, Christofer Bogaso bogaso.christo...@gmail.com wrote: Hello again, Let say I have a matrix: Mat - matrix(1:12, 4, 3) And a vector: Vec - 5:8 Now I want to do following: Each element of row-i in 'Mat' will be divided by i-th element of Vec Is there

Re: [R] simple matrix calculation

2012-03-29 Thread Petr Savicky
On Wed, Mar 28, 2012 at 10:46:11PM +0200, Kehl Dániel wrote: Dear list-members, I have a 9-by-9 matrix lets call it A with first row a11, a12, a13,..., a19 etc. I also have a vector of length 3 (B). I want to construct a matrix of size 3x3 in the following way: - divide matrix A to 9 3x3

Re: [R] simple matrix calculation

2012-03-29 Thread Kehl Dániel
Dear David, Ted, Kjetil, Petr, thank you, you guys did a great job, I'll use your ideas in the future for sure. After I sent the question I figured a way, see below. x - 1:81 b - 1:3 Q - matrix(x,9,9) result - matrix(matrix(colSums(matrix(t(Q),3)),,3,TRUE) %*% b,3,3) I hope there is no error

Re: [R] simple matrix calculation

2012-03-29 Thread Berend Hasselman
On 29-03-2012, at 13:16, Kehl Dániel wrote: Dear David, Ted, Kjetil, Petr, thank you, you guys did a great job, I'll use your ideas in the future for sure. After I sent the question I figured a way, see below. x - 1:81 b - 1:3 Q - matrix(x,9,9) result -

Re: [R] simple matrix calculation

2012-03-29 Thread Petr Savicky
On Thu, Mar 29, 2012 at 01:16:49PM +0200, Kehl Dániel wrote: Dear David, Ted, Kjetil, Petr, thank you, you guys did a great job, I'll use your ideas in the future for sure. After I sent the question I figured a way, see below. x - 1:81 b - 1:3 Q - matrix(x,9,9) result -

Re: [R] simple matrix calculation

2012-03-29 Thread Petr Savicky
On Thu, Mar 29, 2012 at 03:13:25PM +0200, Petr Savicky wrote: On Thu, Mar 29, 2012 at 01:16:49PM +0200, Kehl Dániel wrote: Dear David, Ted, Kjetil, Petr, thank you, you guys did a great job, I'll use your ideas in the future for sure. After I sent the question I figured a way, see

[R] simple matrix calculation

2012-03-28 Thread Kehl Dániel
Dear list-members, I have a 9-by-9 matrix lets call it A with first row a11, a12, a13,..., a19 etc. I also have a vector of length 3 (B). I want to construct a matrix of size 3x3 in the following way: - divide matrix A to 9 3x3 blocks - first is a11, a12, a13 a21, a22, a23

Re: [R] simple matrix calculation

2012-03-28 Thread Ted Harding
On 28-Mar-2012 Kehl Dániel wrote: Dear list-members, I have a 9-by-9 matrix lets call it A with first row a11, a12, a13,..., a19 etc. I also have a vector of length 3 (B). I want to construct a matrix of size 3x3 in the following way: - divide matrix A to 9 3x3 blocks - first is

Re: [R] simple matrix calculation

2012-03-28 Thread David Winsemius
On Mar 28, 2012, at 4:46 PM, Kehl Dániel wrote: Dear list-members, I have a 9-by-9 matrix lets call it A with first row a11, a12, a13,..., a19 etc. I also have a vector of length 3 (B). I want to construct a matrix of size 3x3 in the following way: - divide matrix A to 9 3x3 blocks - first

Re: [R] simple matrix calculation

2012-03-28 Thread Kjetil Halvorsen
see inline. On Wed, Mar 28, 2012 at 2:46 PM, Kehl Dániel ke...@ktk.pte.hu wrote: Dear list-members, I have a 9-by-9 matrix lets call it A with first row a11, a12, a13,..., a19 etc. I also have a vector of length 3 (B). I want to construct a matrix of size 3x3 in the following way: - divide

[R] A matrix calculation

2009-08-23 Thread Bogaso
I have suppose a matrix like that mat - matrix(1:21, 7) mat [,1] [,2] [,3] [1,]18 15 [2,]29 16 [3,]3 10 17 [4,]4 11 18 [5,]5 12 19 [6,]6 13 20 [7,]7 14 21 From this matrix, I want to create a vector like tha : c(mat[7,],

Re: [R] A matrix calculation

2009-08-23 Thread David Winsemius
On Aug 23, 2009, at 2:37 PM, Bogaso wrote: I have suppose a matrix like that mat - matrix(1:21, 7) mat [,1] [,2] [,3] [1,]18 15 [2,]29 16 [3,]3 10 17 [4,]4 11 18 [5,]5 12 19 [6,]6 13 20 [7,]7 14 21 From this matrix, I want

Re: [R] A matrix calculation

2009-08-23 Thread Bogaso
No no, I actually want following result : 7, 14, 21, 6, 13, 20, 5, 12, 19, David Winsemius wrote: On Aug 23, 2009, at 2:37 PM, Bogaso wrote: I have suppose a matrix like that mat - matrix(1:21, 7) mat [,1] [,2] [,3] [1,]18 15 [2,]2

Re: [R] A matrix calculation

2009-08-23 Thread Peter Dalgaard
Bogaso wrote: No no, I actually want following result : 7, 14, 21, 6, 13, 20, 5, 12, 19, c(t(mat[7:1,])) then. David Winsemius wrote: On Aug 23, 2009, at 2:37 PM, Bogaso wrote: I have suppose a matrix like that mat - matrix(1:21, 7) mat [,1] [,2] [,3]

Re: [R] A matrix calculation

2009-08-23 Thread Michael Knudsen
On Sun, Aug 23, 2009 at 8:53 PM, Bogasobogaso.christo...@gmail.com wrote: No no, I actually want following result : 7,   14,   21, 6,   13,   20, 5,   12,   19, How about this? x = c() for (i in 7:1) x = c(x,mat[i,]) Guess that would do the trick. Best, Michael -- Michael

Re: [R] A matrix calculation

2009-08-23 Thread David Winsemius
On Aug 23, 2009, at 2:53 PM, Bogaso wrote: No no, I actually want following result : 7, 14, 21, 6, 13, 20, 5, 12, 19, Ooops. That is what I thought you wanted, but I didn't check very carefully, did I? c(apply(mat[7:1,],1,I) ) # the I() function just returns

Re: [R] A matrix calculation

2009-08-23 Thread Ted Harding
The problem with David's proposal is revealed by: mat[7:1,] # [,1] [,2] [,3] # [1,]7 14 21 # [2,]6 13 20 # [3,]5 12 19 # [4,]4 11 18 # [5,]3 10 17 # [6,]29 16 # [7,]18 15 which simply reverses the rows. Then:

Re: [R] A matrix calculation

2009-08-23 Thread David Winsemius
There is another matrix strategy that succeeds, although it is clearly less economical that the transpose approach: matrix(mat[7:1, ], ncol=nrow(mat), byrow=TRUE) # will transpose the matrix I offer this only as a reminder that the byrow= parameter is available when appropriate. --

Re: [R] A matrix calculation

2009-08-23 Thread Erich Neuwirth
as.vector(t(mat[7:1,])) On Aug 23, 2009, at 9:35 PM, David Winsemius wrote: There is another matrix strategy that succeeds, although it is clearly less economical that the transpose approach: matrix(mat[7:1, ], ncol=nrow(mat), byrow=TRUE) # will transpose the matrix I offer this only