[R] Odp: Multiplying each row of a big matrix with a vector

Petr PIKAL petr.pikal at precheza.cz
Wed Jan 30 08:53:43 CET 2008


I am not sure if you really want what you explain that you want

To multiply each row with a vector you can use e.g.

t(apply(ret,1, function(y) pos*y))

or

t(t(ret)*pos)

but then you will get the same matrix just multiplied by a vector.

If you want to perform matrix multiplication then just

ret %*% pos will do it for you

Petr Pikal
petr.pikal at precheza.cz

r-help-bounces at r-project.org napsal dne 30.01.2008 08:20:21:

> I have a big matrix 'ret'. I want to multiply each row of it with a 2nd 
vector
> 'pos', resulting result, I want to save in a vector named 'port'. I 
wrote 
> following code:
> 
>   > pos
> [1]  2593419  2130220  6198197  1673888  1980000  1784732  2052120 
-7490228 -5275000
> 
> 
>   > dim(ret)
> [1] 500   9
> 
>   > fu    # user defined function
> function(x)
>    {
>     fu = x %*% t(pos)
>    }
> port = apply(ret, 1, fu)
> 
>   > dim(port)
> [1]  81 500
> 
>   My desire is to get port as a vector with length 500. However I am not 
getting that?
> 
>   Can anyone tell me how to correct that? 
> 
>   Regards,
> 
> 
> 
> ---------------------------------
> 
>    [[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.



More information about the R-help mailing list