[R] matrix column division by vector

Ted Harding Ted.Harding at wlandres.net
Wed May 14 19:16:15 CEST 2014


Maybe I am missing the point -- but what is wrong with line 3 of:

  m=rbind(c(6,4,2),c(3,2,1))
  v= c(3,2,1)
  m%*%diag(1/v)
  #      [,1] [,2] [,3]
  # [1,]    2    2    2
  # [2,]    1    1    1

Ted.

On 14-May-2014 15:03:36 Frede Aakmann Tøgersen wrote:
> Have a look at ?sweep
> 
> Br. Frede
> 
> 
> Sendt fra Samsung mobil
> -------- Oprindelig meddelelse --------
> Fra: carol white
> Dato:14/05/2014 16.53 (GMT+01:00)
> Til: r-help at stat.math.ethz.ch
> Emne: [R] matrix column division by vector
> 
> Hi,
> What is the elegant script to divide the columns of a matrix by the
> respective position of a vector elements?
> 
> m=rbind(c(6,4,2),c(3,2,1))
> 
> v= c(3,2,1)
> 
> res= 6/3   4/2  2/1
>         3/3   2/2    1/1
> 
> 
> this is correct
> mat2 = NULL
> 
> for (i in 1: ncol(m))
> 
>     mat2 = cbind(mat2, m[,i]/ v[i])
> 
> 
> but how to do more compact and elegant with for ex do.call?
> 
> Many thanks
> 
> Carol
>         [[alternative HTML version deleted]]
> 
> 
>       [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.

-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 14-May-2014  Time: 18:16:12
This message was sent by XFMail



More information about the R-help mailing list