[R] Equivalent to matlab ".*" operator in R
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Wed Nov 19 16:24:48 CET 2014
When your matrices are the same size, the "*" operator does what you want. The problem is that you have to make a conforming version of z before you can use that operator.
y*matrix(rep(z,3),ncol=2,byrow=TRUE)
or
y*matrix(rep(z,each=3),ncol=2)
To interpret this, just keep in mind that matrices are folded vectors in R... every matrix can be thought of as a linear vector of columnwise data with dimension attributes.
---------------------------------------------------------------------------
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 November 19, 2014 6:43:55 AM PST, "Ruima E." <ruimaximo at gmail.com> wrote:
>Thank you Chel Hee.
>
>Isn't there a simpler way to do so?
>
>
>On Wed, Nov 19, 2014 at 3:35 PM, Chel Hee Lee <chl948 at mail.usask.ca>
>wrote:
>>> y = matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2)
>>> z = matrix(c(12, -6),ncol=2)
>>> t(apply(y, 1, function(x) x*z))
>> [,1] [,2]
>> [1,] 0 0
>> [2,] 6 -3
>> [3,] 12 -6
>>
>> I hope this helps.
>>
>> Chel Hee Lee
>>
>> On 14-11-19 08:22 AM, Ruima E. wrote:
>>> Hi,
>>>
>>> I have this:
>>>
>>> y = matrix(cbind(c(0, 0.5, 1),c(0, 0.5, 1)),ncol=2)
>>> z = matrix(c(12, -6),ncol=2)
>>>
>>> In matlab I would do this
>>>
>>>> y .* x
>>> I would get this in matlab
>>>
>>>> ans
>>> 0 -0
>>> 6 -3
>>> 12 -6
>>>
>>> What is the equivalent in R?
>>>
>>> Thanks
>>>
>>> [[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.
>>
>
>______________________________________________
>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