Matrix operation with apply family functions

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Matrix operation with apply family functions

Dimitris.Kapetanakis
Dear all,

I am trying to use the apply family functions to improve the efficiency of my code, though it is a bit hard for me to find sometimes the solutions with these functions.

The problem that I want to solve is:

I have a matrix of replicated random generated data, e.g. if I have two controls with 100 observations and replicated the data 100 times then I am going to have a 100x2000 matrix (odd rows have the control x1 and even rows control x2). Then I want to multiply a vector of order two in this example (q in general) by each observation of the random generated data, so at the end I am going to obtain a 100x1000 matrix.

A solution to the above problem is given by the following code without using apply family functions and is efficient I think as well, but I would like to know if there is another way because I think the other way can solve similar problems that I have and this particular way cannot solve.

x.1<-replicate(1000, rnorm(100,0,1))
x.2<-replicate(1000, rnorm(100,1,1))

Z<-BETA[1,]*x.1+BETA[2,]*x.2

Potential set up of the problem I want to solve with the apply family functions:

X<-replicate(1000, c(rnorm(n,0,1), rnorm(n,1,1)))
X<-matrix(X, nrow=100, ncol=2000)

Z<-apply(X, ...???????......)or even better another family apply function????

Thank you

Dimitris