title: MATLAB: Remove specific elements from array
body: <p><strong>Question 1:</strong> I have a 1x15 array, comprising of positive integers and negative integers. I wish to implement a MATLAB code which keeps all positive integers and skips the cells with negative contents.</p>
  <p>I have tried the following:</p>
  <pre><code>X = [1 2 3 4 5 -10 1 -5 4 6 8 9 2 4 -2];
  [r c] = size(X);
  for i=1:r
      for j=1:c
          if X(i,j)&lt;0
              X(i,j)=X(i,j+1)
          end
      end
  end
  </code></pre>
  <p>The output should be:</p>
  <pre><code>X_new = [1 2 3 4 5 1 4 6 8 9 2 4]
  </code></pre>
  <p>How do I do this?</p>
  <hr />
  <p><strong>Question 2:</strong></p>
  <pre><code>X = [1 2 3 4 5 -10 1 -5 4 6 8 9 2 4 -2]
  Y = [5 3 8 9 4  5 6  7 4 7 9 5 2 1  4]
  </code></pre>
  <p>From Question 1,</p>
  <pre><code>X_new = [1 2 3 4 5 1 4 6 8 9 2 4]
  </code></pre>
  <p>I need to delete the corresponding values in <code>Y</code> so that:</p>
  <pre><code>Y_new = [5 3 8 9 4 6 4 7 9 5 2 1]
  </code></pre>
  <p>How do I perform this?</p>