title: How to store data from if statement inside a for loop
body: <p>In the for loop, you only can keep the last result from the for loop.  I want to save the data each time I finish from the for loop. I can't put the index of the for loop because not all variables are satisfying the if statement: my code is:</p>
  
  <pre><code>c=[1 3 2 3 3];p=[0 0 1 2 1];level2=[1 3];
  for j=1:length(level2)
      for i=1:length(p)
          a=[p(i),c(i)]';
          if a(1,:)==level2(j)
              level3=a(2)
          else
          end
      end
  end
  </code></pre>
  
  <p>So, how can I make <code>level3</code> as a vector for all results from the for loop?  Note that <code>p</code>,<code>c</code> and <code>level2</code> should be dynamic values, but here they are static to make the example much easier.</p>