title: Out of memory. Type "help memory" for your options
body: <p>I am implementing particle filter in matlab. I am getting the above mentioned error if I try to vectorize my code. However, it works well if I dont vectorize. I need to vectorize it to save time taken by for loop. Any help would be appreciated. I have gone through other posts addressing same issues but however could not get any useful information for my perticular problem.</p>
  
  <p><strong>Vectorized code</strong></p>
  
  <pre><code>n = size(X_kmin1,1);
  
  X_k = proc_f(X_kmin1) + mvnrnd(zeros(n,1),proc_Q)';
  ykp =  meas_h(X_k);
  
  pyx = exp(-0.5 * (yk-ykp)' * ((meas_R)^-1) * (yk-ykp));
  W_k = W_kmin1*pyx;
  
  Wt = sum(W_k);
  W_k = W_k/Wt;
  </code></pre>
  
  <p><strong>Error statement</strong></p>
  
  <pre><code>Out of memory. Type "help memory" for your options. 
  Error in pfFilterStep (line 24) 
  pyx = exp(-0.5 * ((yk-ykp)' * (meas_R)^-1 * (yk-ykp))); 
  Error in Test1 (line 32) 
  [X_k, W_k] = pfFilterStep(X_kmin1, W_kmin1, yk, proc_f, proc_Q, meas_h, meas_R);
  </code></pre>
  
  <p><strong>Working code</strong></p>
  
  <pre><code>%   Input:
  %   X_kmin1     [n x N] Particles for state x in time k-1
  %   W_kmin1     [1 x N] Weights for state x in time k-1
  %   y_k         [m x 1] Measurement vector for time k
  %   proc_f      Handle for process function f(x_k-1)
  %   proc_Q      [n x n] process noise covariance
  %   meas_h      Handle for measurement model function h(x_k)
  %   meas_R      [m x m] measurement noise covariance
  %
  % Output:
  %   X_k         [n x N] Particles for state x in time k
  %   W_k         [1 x N] Weights for state x in time k
  
  % Your code here!
  n = size(X_kmin1,1);
  N = size(X_kmin1,2);
  
  for i = 1:N
      X_k(:,i) = proc_f(X_kmin1(:,i)) + mvnrnd(zeros(n,1),proc_Q)';
      ykp =  meas_h(X_k(:,i));
  
      pyx = exp(-0.5 * ((yk-ykp)' * (meas_R)^-1 * (yk-ykp)));
      W_k(i) = W_kmin1(i)*pyx;
  end
  
  Wt = sum(W_k);
  W_k = W_k/Wt;
  end
  </code></pre>