[R] Equivalent to matlab ".*" operator in R

Ista Zahn istazahn at gmail.com
Wed Nov 19 15:58:55 CET 2014


On Wed, Nov 19, 2014 at 9:50 AM, Berend Hasselman <bhh at xs4all.nl> wrote:
>
> On 19-11-2014, at 15:22, Ruima E. <ruimaximo at gmail.com> 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?
>>
>
> One way of doing this could be:
>
> y * rep(z,1,each=nrow(y))

another is

t(t(y) * as.vector(z))

--Ista

>
> Berend
>
>> 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