title: How can I make this function run quicker?
body: <p>I have written this code that solves a banded matrix using jacobi iteration. It's a modified Jacobi function that is "optimized" to work quicker on banded matrices. I have successfully modified it only compute non zero terms in the matrix, but it runs much slower than the original code. How can I make it run more efficiently?</p>
  
  <p>Original code:</p>
  
  <pre><code>while (true)
  iter = iter+1;
  for r=1:m                           % looping through all rows of A
      sum = 0;                        % setting sum equal to 0
      for c=1:n                       % looping through all columns of A
          if r~=c                     % if the r does not equal c, meaning the current elements are not diagonals...
              sum = sum + A(r,c)*x0(c);% set sum = sum plus current element times cth element of x
          end
  
      x(r) = (-sum + b(r))/A(r,r);    % if r and c are diagonals, the x value at that element is -sum + b(r) divided by that element of A
  
      end
  end
  if abs(norm(x) - norm(x0)) &lt; TOL    % checking tolerance
      break
  end
  x0 = x;                             % setting x0 = x before relooping
  end
  </code></pre>
  
  <p>Banded Code:</p>
  
  <pre><code>while (true)
  %     iter = iter+1; 
  for r=1:m                           % looping through all rows of A  
    for i = 1:hi                      % looping through the section of A at the beginning that does not have a complete "low"
        sum = 0;                      % assigning sum = 0 for each loop
      for r = i                       % looping through rows of first section
         for c = 1:r+hi;              % looping through only columns that go from the first column to hi after the diagona
          if r~=c                     % if the r does not equal c, meaning the current elements are not diagonals...
              sum = sum + A(r,c)*x0(c);% set sum = sum plus current element times cth element of x
            end
      x(r) = (-sum + b(r))/A(r,r);    % solving the row
         end
  
      end
    end
    for i = 1+hi:n-hi                 % looping through second section (section with a complete "low" and "hi" for each diagonal
        sum = 0;                      % setting sum = 0 for each loop
      for r = i                       % looping through each row of second section
         for c = r+low:r+hi;          % looping through only columns of current row that are in the bandwidth
          if r~=c                     % if the r does not equal c, meaning the current elements are not diagonals...
              sum = sum + A(r,c)*x0(c);% set sum = sum plus current element times cth element of x
          end
  
      x(r) = (-sum + b(r))/A(r,r);     % solving current row
         end
  
        end
    end
    for i = n+low+1:n                 % looping through final section of A (section with out a complete "hi" for each diagonal
        sum = 0;                      % setting sum = 0 for each loop
      for r = i                       % looping through each row of final section
         for c = r + low:n;           % looping through only columns of current row that are from "low" away from the diagonal to the end of the row
          if r~=c                     % if the r does not equal c, meaning the current elements are not diagonals...
              sum = sum + A(r,c)*x0(c);% set sum = sum plus current element times cth element of x
          end   
      x(r) = (-sum + b(r))/A(r,r);    % solving current row
         end   
      end
    end
  end
  if abs(norm(x) - norm(x0)) &lt; TOL    % checking tolerance
      break
  end 
     x0 = x;                              % assigning x0 as the new estimate from the previous attempt, x
  end
  sln = x0;                               % assigning output variable
  </code></pre>
  
  <p>The changes I made to it were dividing it up into 3 sections (1 for the section of a band that doesn't have a complete "low" and "hi" at the start, 1 for the same at the end, and 1 for the section in the middle. Then I changed the loop to start at low and go to hi. why is this function taking so long to run? It takes about a minute for a 20x20 matrix.</p>