Muhammad Azam
2008-Oct-30 08:34 UTC
[R] request: How to combine three matrices in the desired form
Dear R-friends I have three matrices e.g. var <- matrix(c(4,4,4,4,0,4,4,4,0,3,3,0),nrow=4); val <- matrix(c(0.6,0.6,0.6,0.6,0,1.6,1.6,1.6,0,4.9,4.9,0),nrow=4); nod <- matrix(c(-1,-1,1,1),ncol=1)> var[,1] [,2] [,3] [1,] 4 0 0 [2,] 4 4 3 [3,] 4 4 3 [4,] 4 4 0> val[,1] [,2] [,3] [1,] 0.6 0.0 0.0 [2,] 0.6 1.6 4.9 [3,] 0.6 1.6 4.9 [4,] 0.6 1.6 0.0> nod[,1] [1,] -1 [2,] -1 [3,] 1 [4,] 1 Always there is same number of rows and columns for "var" and "val". Also no of rows of all three are always same. Combining them we get> cbind(var,val,nod)[,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 4 0 0 0.6 0.0 0.0 -1 [2,] 4 4 3 0.6 1.6 4.9 -1 [3,] 4 4 3 0.6 1.6 4.9 1 [4,] 4 4 0 0.6 1.6 0.0 1 Is there any way to write them in the following form? [,1] [,2] [,3] [,4] [1,] 4(0.6) 0 0 -1 [2,] 4(0.6) 4(1.6) 3(4.9) -1 [3,] 4(0.6) 4(1.6) 3(4.9) 1 [4,] 4(0.6) 4(1.6) 0 1 Any help in this regard will be appreciated. best regards M.Azam [[alternative HTML version deleted]]
Jim Lemon
2008-Oct-30 09:06 UTC
[R] request: How to combine three matrices in the desired form
Hi Muhammad, Try this: cbind(matrix(paste(var,"(",val,")",sep=""),nrow=4),as.character(nod)) Jim
Muhammad Azam
2008-Oct-30 09:09 UTC
[R] request: How to combine three matrices in the desired form
Dear Jim Thanks a lot for such a nice solution. best regards M.Azam ________________________________ From: Jim Lemon <jim@bitwrit.com.au> Cc: R Help <r-help@r-project.org> Sent: Thursday, October 30, 2008 10:06:57 AM Subject: Re: [R] request: How to combine three matrices in the desired form Hi Muhammad, Try this: cbind(matrix(paste(var,"(",val,")",sep=""),nrow=4),as.character(nod)) Jim [[alternative HTML version deleted]]