[R] Multiplication usijg apply

Berend Hasselman bhh at xs4all.nl
Tue Feb 21 08:28:30 CET 2012


On 21-02-2012, at 04:41, arunkumar1111 wrote:

> Hi
> 
> my data
> 
> looks like 
> 
> X=(3,5,6,7)    B= 1 2 3 4       and C=( 1 2 3 4)
>                              2 3 4 5
>                              4 5 6 7
>                              8 9 1 2
> 
> 
> I want my results to be 
> 
> XB + C.
> 
> Please help me use apply in this function
> 
Why apply?
Maybe this is what you want:

> X<- c(3,5,6,7)   
> B<- matrix(c(1, 2, 3, 4,  
+              2, 3, 4, 5,
+              4, 5, 6, 7,
+              8, 9, 1, 2), nrow=4,byrow=TRUE)
> C <- c(1, 2, 3, 4)
>
> matrix(X, nrow=1) %*% B %*% matrix(C,ncol=1)
     [,1]
[1,]  909

Berend



More information about the R-help mailing list