title: How can I avoid if else statements within a for loop?
body: <p>I have a code that yields a solution similar to the desired output, and I don't know how to perfect this.</p>
  
  <p>The code is as follows.</p>
  
  <pre><code>N = 4; % sampling period
  for nB = -30:-1; 
    if rem(nB,N)==0
      xnB(abs(nB)) = -(cos(.1*pi*nB)-(4*sin(.2*pi*nB))); 
    else
      xnB(abs(nB)) = 0;
    end
  end
  for nC = 1:30; 
    if rem(nC,N)==0
      xnC(nC) = cos(.1*pi*nC)-(4*sin(.2*pi*nC)); 
    else
      xnC(nC) = 0;
    end
  end
  nB = -30:-1; 
  nC = 1:30;
  nD = 0;
  xnD = 0;
  plot(nA,xnA,nB,xnB,'r--o',nC,xnC,'r--o',nD,xnD,'r--o')
  </code></pre>
  
  <p>This produces something that is close, but not close enough for proper data recovery.</p>
  
  <p>I have tried using an index that has the same length but simply starts at <code>1</code> but the output was even worse than this, though if that is a viable option please explain thoroughly, how it should be done.</p>
  
  <p>I have tried running this in a single <code>for-loop</code> with one <code>if-statement</code> but there is a problem when the counter passes zero. What is a way around this that would allow me to avoid using two <code>for-loops</code>? (I'm fairly confident that, solving this issue would increase the accuracy of my output enough to successfully recover the signal.)</p>
  
  <p><strong>EDIT/CLARIFICATION/ADD - 1</strong></p>
  
  <p>I do in fact want to evaluate the signal at the index of zero. The <code>if-statement</code> cannot handle an index of zero which is an index that I'd prefer not to skip.</p>
  
  <p>The goal of this code is to be able to sample a signal, and then I will build a code that will put it through a recovery filter.</p>
  
  <p><strong>EDIT/UPDATE - 2</strong></p>
  
  <pre><code>nA = -30:.1:30; % n values for original function
  xnA = cos(.1*pi*nA)-(4*sin(.2*pi*nA)); % original function
  N = 4; % sampling period
  n = -30:30;
  xn = zeros(size(n));
  xn(rem(n,N)==0) = -(cos(.1*pi*n)-(4*sin(.2*pi*n)));
  plot(nA,xnA,n,xn,'r--o')
  title('Original seq. x and Sampled seq. xp')
  xlabel('n')
  ylabel('x(n) and xp(n)')
  legend('original','sampled');
  </code></pre>
  
  <p>This threw an error at the line <code>xn(rem(n,N)==0) = -(cos(.1*pi*n)-(4*sin(.2*pi*n)));</code> which read: <strong>In an assignment  A(I) = B, the number of elements in B and I must be the same.</strong> I have ran into this error before, but my previous encounters were usually the result of faulty looping. Could someone point out why it isn't working this time?</p>
  
  <p><strong>EDIT/Clarification - 3</strong></p>
  
  <pre><code>N = 4; % sampling period
  for nB = -30:30;
      if rem(nB,N)==0
          xnB(abs(nB)) = -(cos(.1*pi*nB)-(4*sin(.2*pi*nB)));
      else
          xnB(abs(nB)) = 0; 
      end
  end
  </code></pre>
  
  <p>The error message resulting is as follows: <strong>Attempted to access xnB(0); index must be a positive integer or logical.</strong></p>
  
  <p><strong>EDIT/SUCCESS - 4</strong></p>
  
  <p>After taking another look at the answers posted, I realized that the negative sign in front of the cos function wasn't supposed to be in the original coding.</p>