title: Sliced Variables in PARFOR loop: Sequential to Parallel Conversion in MATLAB
body: <p>I have a code in MATLAB in which I'm running monte-carlo simulations using <em>parfor</em> instead of simple <em>for</em> loop to convert the code from sequential to parallel. Following is the piece of code which is inside the <em>parfor</em> loop. </p>
  
  <p>But MATLAB gives an error saying "Valid indices for local_Q_mega_sub_seed are restricted in <em>parfor</em> loop". Suggested action says to "Fix the index" and it suggests to use "Sliced Variables". I've been struggling to use this concept. I have read <a href="https://blogs.mathworks.com/loren/2009/10/02/using-parfor-loops-getting-up-and-running/#12" rel="nofollow noreferrer">https://blogs.mathworks.com/loren/2009/10/02/using-parfor-loops-getting-up-and-running/#12</a> and <a href="https://www.mathworks.com/matlabcentral/answers/123922-sliced-variables-in-parfor-loop-restricted-indexing" rel="nofollow noreferrer">https://www.mathworks.com/matlabcentral/answers/123922-sliced-variables-in-parfor-loop-restricted-indexing</a> along with MATLAB documentation <a href="https://www.mathworks.com/help/distcomp/sliced-variable.html" rel="nofollow noreferrer">https://www.mathworks.com/help/distcomp/sliced-variable.html</a> and <a href="https://www.mathworks.com/help/distcomp/parfor.html" rel="nofollow noreferrer">https://www.mathworks.com/help/distcomp/parfor.html</a> but I'm not getting it right.</p>
  
  <p>Could anyone please let me know how can I use sliced variables in the given piece of code so that I could get an idea?</p>
  
  <pre><code>index_f = 1;
  subseed_step = (sub_seed_transmitted_at/fs_local)*sintablen_mega_frequency;
  for i = 1 : fs_local
  local_Q_mega_sub_seed(i) = SINTAB(round(index_f));
  local_I_mega_sub_seed(i) = COSTAB(round(index_f));
  index_f = index_f + subseed_step;
  if index_f&gt;sintablen_mega_frequency
      index_f = index_f - sintablen_mega_frequency;
  end
  </code></pre>