title: What are the ways to sum matrix elements in MATLAB?
body: <p>Given the matrix:</p>
  
  <pre><code>A = [1 2 3; 4 5 6; 7 8 9];
  </code></pre>
  
  <ol>
  <li>How could you use a for loop to compute the sum of the elements in the matrix?</li>
  <li>Write a one line MATLAB command using the function <code>sum</code> to sum the
  matrix elements in <code>A</code>.</li>
  </ol>
  
  <h3>My answer:</h3>
  
  <p>1) </p>
  
  <pre><code>for j=1:3,
      for i=j:3,
          A(i,:) = A(i,:)+A(j+1,:)+A(j+2,:)
      end
  end
  </code></pre>
  
  <p>2)</p>
  
  <pre><code>sum(A)
  </code></pre>
  
  <p>Are these the correct answers? I didn't know how to use <code>if</code>, <code>while</code> and <code>for</code>. Can anyone explain it to me?</p>