I like to multiple the first and second column of a 10 x 3 matrix by 100. The following did not work. I need this in an operation with a much larger scale. Any help? aa<-matrix(1:30,nrow=10,ncol=3); aa bb<-matrix(c(100,100,1),nrow=1,ncol=3); bb dim(aa) dim(bb) aa*bb Results: > aa<-matrix(1:30,nrow=10,ncol=3); aa [,1] [,2] [,3] [1,] 1 11 21 [2,] 2 12 22 [3,] 3 13 23 [4,] 4 14 24 [5,] 5 15 25 [6,] 6 16 26 [7,] 7 17 27 [8,] 8 18 28 [9,] 9 19 29 [10,] 10 20 30 > bb<-matrix(c(100,100,1),nrow=1,ncol=3); bb [,1] [,2] [,3] [1,] 100 100 1 > dim(aa) [1] 10 3 > dim(bb) [1] 1 3 > aa*bb Error in aa * bb : non-conformable arrays >
You can create a suitable matrix bb as below (note the byrow = TRUE argument) aa<-matrix(1:30,nrow=10,ncol=3); aa bb<-matrix(c(100,100,1),nrow=10,ncol=3, byrow = TRUE); bb dim(aa) dim(bb) aa * bb You can also use matrix multiplication, but that;s slightly more involved: aa<-matrix(1:30,nrow=10,ncol=3); aa bb<-matrix(0,nrow=3,ncol=3); diag(bb) = c(100,100,1); bb dim(aa) dim(bb) aa %*% bb HTH, Peter On Wed, Jan 7, 2015 at 3:05 PM, Steven Yen <syen04 at gmail.com> wrote:> I like to multiple the first and second column of a 10 x 3 matrix by 100. > The following did not work. I need this in an operation with a much larger > scale. Any help? > > aa<-matrix(1:30,nrow=10,ncol=3); aa > bb<-matrix(c(100,100,1),nrow=1,ncol=3); bb > dim(aa) > dim(bb) > aa*bb > > Results: > >> aa<-matrix(1:30,nrow=10,ncol=3); aa > [,1] [,2] [,3] > [1,] 1 11 21 > [2,] 2 12 22 > [3,] 3 13 23 > [4,] 4 14 24 > [5,] 5 15 25 > [6,] 6 16 26 > [7,] 7 17 27 > [8,] 8 18 28 > [9,] 9 19 29 > [10,] 10 20 30 >> bb<-matrix(c(100,100,1),nrow=1,ncol=3); bb > [,1] [,2] [,3] > [1,] 100 100 1 >> dim(aa) > [1] 10 3 >> dim(bb) > [1] 1 3 >> aa*bb > Error in aa * bb : non-conformable arrays > >> > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
On Wed, Jan 7, 2015 at 5:05 PM, Steven Yen <syen04 at gmail.com> wrote:> I like to multiple the first and second column of a 10 x 3 matrix by 100. > The following did not work. I need this in an operation with a much larger > scale. Any help? > > aa<-matrix(1:30,nrow=10,ncol=3); aa > bb<-matrix(c(100,100,1),nrow=1,ncol=3); bb > dim(aa) > dim(bb) > aa*bb > > Results: > > > aa<-matrix(1:30,nrow=10,ncol=3); aa > [,1] [,2] [,3] > [1,] 1 11 21 > [2,] 2 12 22 > [3,] 3 13 23 > [4,] 4 14 24 > [5,] 5 15 25 > [6,] 6 16 26 > [7,] 7 17 27 > [8,] 8 18 28 > [9,] 9 19 29 > [10,] 10 20 30 > > bb<-matrix(c(100,100,1),nrow=1,ncol=3); bb > [,1] [,2] [,3] > [1,] 100 100 1 > > dim(aa) > [1] 10 3 > > dim(bb) > [1] 1 3 > > aa*bb > Error in aa * bb : non-conformable arrays > > > >?Assuming that this is exactly what you want to do, then aa[,1:2]<-aa[,1:2]*100; transcript:> aa<-matrix(1:30,nrow=10,ncol=3); > aa [,1] [,2] [,3][1,] 1 11 21 [2,] 2 12 22 [3,] 3 13 23 [4,] 4 14 24 [5,] 5 15 25 [6,] 6 16 26 [7,] 7 17 27 [8,] 8 18 28 [9,] 9 19 29 [10,] 10 20 30> aa[,1:2]<-aa[,1:2]*100> aa [,1] [,2] [,3] [1,] 100 1100 21 [2,] 200 1200 22 [3,] 300 1300 23 [4,] 400 1400 24 [5,] 500 1500 25 [6,] 600 1600 26 [7,] 700 1700 27 [8,] 800 1800 28 [9,] 900 1900 29 [10,] 1000 2000 30>? -- ? While a transcendent vocabulary is laudable, one must be eternally careful so that the calculated objective of communication does not become ensconced in obscurity. In other words, eschew obfuscation. 111,111,111 x 111,111,111 = 12,345,678,987,654,321 Maranatha! <>< John McKown [[alternative HTML version deleted]]
On Wed, Jan 7, 2015 at 3:15 PM, Peter Langfelder <peter.langfelder at gmail.com> wrote:> You can create a suitable matrix bb as below (note the byrow = TRUE argument) > > aa<-matrix(1:30,nrow=10,ncol=3); aa > bb<-matrix(c(100,100,1),nrow=10,ncol=3, byrow = TRUE); bb > dim(aa) > dim(bb) > aa * bb > > > You can also use matrix multiplication, but that;s slightly more involved:I should add that it will also be much slower if, as you say, you do it on a much larger scale and the dimensions of bb are large. Peter
Thank you both. Both John and Peter's suggestions work great!! At 06:17 PM 1/7/2015, John McKown wrote:>On Wed, Jan 7, 2015 at 5:05 PM, Steven Yen ><<mailto:syen04 at gmail.com>syen04 at gmail.com> wrote: >I like to multiple the first and second column >of a 10 x 3 matrix by 100. The following did not >work. I need this in an operation with a much larger scale. Any help? > >aa<-matrix(1:30,nrow=10,ncol=3); aa >bb<-matrix(c(100,100,1),nrow=1,ncol=3); bb >dim(aa) >dim(bb) >aa*bb > >Results: > > > aa<-matrix(1:30,nrow=10,ncol=3); aa >? ? ? [,1] [,2] [,3] >? [1,]? ? 1? ? 11? ? 21 >? [2,]? ? 2? ? 12? ? 22 >? [3,]? ? 3? ? 13? ? 23 >? [4,]? ? 4? ? 14? ? 24 >? [5,]? ? 5? ? 15? ? 25 >? [6,]? ? 6? ? 16? ? 26 >? [7,]? ? 7? ? 17? ? 27 >? [8,]? ? 8? ? 18? ? 28 >? [9,]? ? 9? ? 19? ? 29 >[10,]? ? 10? ? 20? ? 30 > > bb<-matrix(c(100,100,1),nrow=1,ncol=3); bb >? ? ? [,1] [,2] [,3] >[1,]? 100? 100? ? 1 > > dim(aa) >[1] 10? 3 > > dim(bb) >[1] 1 3 > > aa*bb >Error in aa * bb : non-conformable arrays > > > > > >???Assuming that this is exactly what you want to do, then? > >aa[,1:2]<-aa[,1:2]*100; > >transcript: > > > aa<-matrix(1:30,nrow=10,ncol=3); > > aa > [,1] [,2] [,3] > [1,] 1 11 21 > [2,] 2 12 22 > [3,] 3 13 23 > [4,] 4 14 24 > [5,] 5 15 25 > [6,] 6 16 26 > [7,] 7 17 27 > [8,] 8 18 28 > [9,] 9 19 29 >[10,] 10 20 30 > > aa[,1:2]<-aa[,1:2]*100 > > aa > [,1] [,2] [,3] > [1,] 100 1100 21 > [2,] 200 1200 22 > [3,] 300 1300 23 > [4,] 400 1400 24 > [5,] 500 1500 25 > [6,] 600 1600 26 > [7,] 700 1700 27 > [8,] 800 1800 28 > [9,] 900 1900 29 >[10,] 1000 2000 30 > > > >??? >? > >-- >??? >While a transcendent vocabulary is laudable, one >must be eternally careful so that the calculated >objective of communication does not become >ensconced in obscurity.? In other words, eschew obfuscation. > >111,111,111 x 111,111,111 = 12,345,678,987,654,321 > >Maranatha! <>< >John McKown[[alternative HTML version deleted]]