[R] How to doulbe all the value on a matrix

ADias diasandre at gmail.com
Mon Jan 17 14:53:31 CET 2011


Hi,

Is there an expression to double the values of a matrix - without using a
loop?
 
What I need is this:

Suppose we have this matrix

> m
     [,1] [,2] [,3] 
[1,]    7   17    4   
[2,]   11   10   18    
[3,]   15   19   18   

and I want this matrix

     [,1] [,2] [,3]
[1,]  112  102  115
[2,]  108  109  101
[3,]  104  100  101

where for instance, m[1,1] was obtained by adding
(7+17+4+11+10+18+15+19+18)-7

with this loop I am able to get the result I need but I wanted to know if a
more R way of doing this.

> a<-matrix(c(7,17,4,11,10,18,15,19,18),3,3,T)
> m=a
> for(i in 1:9){
+ m[c(i)]<-sum(a)-a[c(i)]
+ }
> m


thanks
AD
-- 
View this message in context: http://r.789695.n4.nabble.com/How-to-doulbe-all-the-value-on-a-matrix-tp3221213p3221213.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list