[R] matrix column division by vector

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Wed May 14 20:40:02 CEST 2014


Scores well on notational simplicity.

Loses big on computational efficiency (lots of terms multiplied with zero, then added to the one nonzero term).
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On May 14, 2014 10:16:15 AM PDT, Ted Harding <Ted.Harding at wlandres.net> wrote:
>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
>
>______________________________________________
>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.



More information about the R-help mailing list