title: making gradient and del2 in matlab even faster
body: <p>I have a few questions. Bear with me please. I wrote down a program and am trying to optimize it. I am posting only the code snippets that I believe that can be further optimized. </p>
  
  <p><strong>1.</strong> First of all I am trying to calculate the gradient of an image. I previously used the <strong>gradient operator</strong> of the MATLAB but found it to be slow. So I wrote down my own code. Please find my code at <a href="https://stackoverflow.com/questions/18958231/fast-computation-of-a-gradient-of-an-image-in-matlab">my gradient code here!</a> </p>
  
  <p>I ran the profiler on MATLAB and found that my program still had a few bottlenecks. I am posting an image here : 
  <img src="https://i.stack.imgur.com/T7mpU.png" alt="gradient profile"></p>
  
  <p>As you can see I am using two extra matrices A and B two times each for the calculation of the gradient in the horizontal &amp; vertical direction. Is it really necessary ? Can't the operation be integrated in line 5 &amp; line 8 without the use of extra matrices A &amp; B. Will the removal of the extra matrices A &amp; B make <strong>my code run faster ?</strong> </p>
  
  <p><strong>2.</strong> Secondly I needed to use the Laplacian operator in my code and found it to be one of the bottlenecks of my program. I have already read the matlab documentation &amp; also read the links <a href="https://stackoverflow.com/questions/12353087/discrepancy-between-matlab-del2-and-matlab-gradient-of-gradient">Discrepancy between Matlab del2 and Matlab gradient of gradient</a> and <a href="https://stackoverflow.com/questions/13342651/understanding-del2-function-in-matlab-in-order-to-code-it-in-c">Understanding DEL2 function in Matlab in order to code it in C++</a>. </p>
  
  <p>I then checked the profile of del2 and found this. <img src="https://i.stack.imgur.com/yF9VS.png" alt="enter image description here">. My question is this what are the functions of the children function <strong>parse_inputs</strong> and <strong>ipermute</strong> in this case ? 
  It is to be noted that I am working with an image matrix whose size will always be greater than 64X64.</p>
  
  <p>I read the explanation for permute and it said that "<strong>Permute so that the del2 is always taken along the columns.</strong>". What does it mean actually ? I read somewhere that MATLAB uses column major format . Could anyone please explain what it means ? </p>
  
  <p>Regarding the parse_inputs sub-function  I read the explanation where it stated that </p>
  
  <pre><code> %PARSE_INPUTS
                    %   [ERR,F,LOC,CFLAG] = PARSE_INPUTS(F,V) returns the spacing LOC
                    %   along the x,y,z,... directions and a column vector flag CFLAG. ERR
                    %   will be true if there is an error.
  </code></pre>
  
  <p>I have no idea what this function does. Please can anyone explain ? </p>
  
  <p><strong>3</strong>. I also tried to write down my own del2 function for matlab so as to make it faster but strangely I could only cope with the interior points. Is the implementation in MATLAB correct for del2 ? Why does it do extrapolation at the boundary points ? </p>
  
  <p>Should not the basis of the Laplacian operator be always that it should hold the equality ? </p>
  
  <pre><code>laplacian = del2(image);
  
  [x, y] = gradient(image);
  [xx, xy] = gradient(x);
  [yx, yy] = gradient(y);
  laplacian = xx + yy;
  </code></pre>
  
  <p>This code was taken from <a href="https://stackoverflow.com/questions/12353087/discrepancy-between-matlab-del2-and-matlab-gradient-of-gradient">Please check code</a>. I read all the posts there but the explanation did not satisfy me. Please could you explain why the above code does not give the same output ? </p>
  
  <p>Thanks everyone for reading this long and boring set of questions. Any help will be greatly appreciated. If anyone has a problem I will split the questions and post it separately. Just say so guys!</p>
  
  <p>Thanks in advance !</p>
  
  <p><strong>EDIT :</strong> After slogging through the whole day finally managed to remove the <em>offending</em> matrices A &amp; B. I am writing down the code below : </p>
  
  <pre><code>function [dx dy] = gradient_my_code(I)
  dx=[I(:,2)-I(:,1) (I(:,3:end)-I(:,1:end-2))./2 I(:,end)-I(:,end-1)];
  dy=[I(2,:)-I(1,:) ; (I(3:end,:)-I(1:end-2,:))./2 ; I(end,:)-I(end-1,:)];
  </code></pre>
  
  <p>For <strong>timing purposes</strong> I did this : </p>
  
  <pre><code>xaxis = [10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000,2000,3000,4000,5000];
  n1 = zeros(1,length(xaxis));
  n2 = zeros(1,length(xaxis));
  for i=1:length(xaxis);
      x=xaxis(i);
      u=magic(x);
      tic
      [dx dy] = gradient(u);
      n1(i)=toc;
      tic
      [dx dy] = gradient_my_code(u);
      n2(i)=toc;
  end
  plot(n1,'r','LineWidth',3);
  hold on;
  plot(n2,'b','LineWidth',3);
  hold off;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/VBMux.jpg" alt="Speed comparison between gradient of matlab and my code"></p>
  
  <p>And I plotted the speed comparison . Please check and guys give your comments.</p>