title: how to Improve the speed matlab
body: <p>This is my matlab code. It runs too slow and I had no clue how to improve it.</p>
  
  <p>Could you help me to improve the speed?</p>
  
  <p>What I would like to do is to create some random points and then remove the random points to make them similar to my target points.</p>
  
  <pre><code>syms Dx Dy p q;
  
  a = 0;
  
  num = 10;
  x = rand(1,num);
  y = rand(1,num);
  figure(1)
  scatter(x,y,'.','g')
  
  %num_x = xlsread('F:\bin\test_2');% num 1024
  %figure(2)
  %scatter(num_x(:,1),num_x(:,2),'.','r');
  
  q = 0;
  num_q = 10;
  
  x_q = randn(1,num_q);
  y_q = randn(1,num_q);
  
  %figure(2)
  hold on;
  scatter(x_q,y_q,'.','r')
  
  for i = 1:num_q;
      for j = 1:num_q;
          qx(i,j) = x_q(i) - x_q(j);
          qy(i,j) = y_q(i) - y_q(j);       
          %qx(i,j) = num_x(i,1) - num_x(j,1);
          %qy(i,j) = num_x(i,2) - num_x(j,2);
          %d~(s(i),s(j))
          if ((qx(i,j))^2+(qy(i,j)^2))&gt; 0.01 % find neighbours
              qx(i,j) = 0;
              qy(i,j) = 0;
          end  
      end
  end
  
  for i = 1:num_q;
      for j = 1:num_q;
          if qx(i,j)&gt;0&amp;&amp;qy(i,j)&gt;0
          q = q + exp(-(((Dx - qx(i,j))^2)+((Dy - qy(i,j))^2))/4);%exp(-(((Dx - qx(i,j))^2)+((Dy - qy(i,j))^2))/4);
          end
      end
  end
  
  %I = ones(num,num); % I(s) should from a grayscale image
  %r = 1./sqrt(I);
  for s = 1:100;
  
  for i = 1:num;
      for j = 1:num;
          dx(i,j) = x(i) - x(j);
          dy(i,j) = y(i) - y(j);
          %d~(s(i),s(j))
          if ((dx(i,j))^2+(dy(i,j)^2))&gt; 0.05 % delta p, find neighbours
              dx(i,j) = 0;
              dy(i,j) = 0;
          end  
      end
  end
  
  p = 0;
  for i = 1:num;
      for j = 1:num;
          if dx(i,j)&gt;0&amp;&amp;dy(i,j)&gt;0
          p = p + exp(-(((Dx - dx(i,j))^2)+((Dy - dy(i,j))^2))/4);
          end
      end
  end
  
  p = p - q;
  sum = 0;
  for i = 1:num;
      for j = 1:num;
          if dx(i,j)&gt;0&amp;&amp;dy(i,j)&gt;0;
          kx(i,j) = (1/2)*(Dx-dx(i,j))*exp((-(Dx-dx(i,j))^2+(Dy-dy(i,j))^2)/4);
          ky(i,j) = (1/2)*(Dy-dy(i,j))*exp((-(Dx-dx(i,j))^2+(Dy-dy(i,j))^2)/4);
          end
      end
  end
  
  sum_x = ones(1,num);% 1行N列0矩阵
  sum_y = ones(1,num);
  
  %fx = zeros(1,num);
  
  
  for i = 1:num;
      for j = 1:num;
          if dx(i,j)&gt;0&amp;&amp;dy(i,j)&gt;0;
              fx(i) = p*kx(i,j);% j is neighbour to i
              fy(i) = p*ky(i,j);
  
              %fx(i) = matlabFunction(fx(i));
              %fy(i) = matlabFunction(fy(i));
              %P =quad2d(@(Dx,Dy) fx,0,0.01,0,0.01);
              %fx =quad(@(Dx) fx,0,0.01);
              %fx(i) =quad(@(Dy) fx(i),0,0.01);
              %Q =quad2d(@(Dx,Dy) fy,0,0.01,0,0.01);
  
              fx(i) = double(int(int(fx(i),Dx,0,0.01),Dy,0,0.01));
              fy(i) = double(int(int(fy(i),Dx,0,0.01),Dy,0,0.01));
  
              %fx(i) = vpa(p*kx(i,j));
              %fy(i) = vpa(p*ky(i,j));
  
  
  
              %fx(i) = dblquad(@(Dx,Dy)fx(i),0,0.01,0,0.01);
              %fy(i) = dblquad(@(Dx,Dy)fy(i),0,0.01,0,0.01);        
              sum_x(i) = sum_x(i) + fx(i);
              sum_y(i) = sum_y(i) + fy(i);
          end
      end
  end
  
  for i = 1:num;
  sum_x = 4.*sum_x./num;
  sum_y = 4.*sum_y./num;
  x(i) = x(i) - 0.05*sum_x(i);
  y(i) = y(i) - 0.05*sum_y(i);
  end
  
  a = a+1
  
  end
  hold on;
  scatter(x,y,'.','b')
  </code></pre>