avoiding loops in R

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

avoiding loops in R

I am working in R language.

I have two arrays denoted E and y. E is indexed as E[state], and y as y[time,sample]. Thus, E is 1 dimensional, and y is 2 dimensional.

I have to calculate the following 3 dimensional array, denoted as P.

P(state,time,sample) = f(E(state),y[time,sample]), where f is some function. I wanted to do this without using loops.

Sample code is as follows.

    # EM algorithm for HMM  
    #y is array containing data
      T #length of time series
      nsamples #number of sequences observed
      nstates # no. of states in HMM
      P_forward<-array(0,dim=c(nstates,T,nsamples)) #forward probabilities in
                                             #EM algo. for HMM
    P_forward[state,time,sample] = 1/abs(E[state]-y[time,sample])