0

I have been running a MATLAB program for almost six hours now, and it is still not complete. It is cycling through three while loops (the outer two loops are n=855, the inner loop is n=500). Is this a surprise that it is taking this long? Is there anything I can do to increase the speed? I am including the code below, as well as the variable data types underneath that.

  while i < (numAtoms + 1)
      pointAccessible = ones(numPoints,1);
      j = 1;
      while j <(numAtoms + 1)
          if (i ~= j)
              k=1;
              while k < (numPoints + 1)
                  if (pointAccessible(k) == 1)
                      sphereCoord = [cell2mat(atomX(i)) + p + sphereX(k), cell2mat(atomY(i)) + p + sphereY(k), cell2mat(atomZ(i)) + p + sphereZ(k)];
                      neighborCoord = [cell2mat(atomX(j)), cell2mat(atomY(j)), cell2mat(atomZ(j))];
                      coords(1,:) = [sphereCoord];
                      coords(2,:) = [neighborCoord];
                      if (pdist(coords) < (atomRadius(j) + p))
                          pointAccessible(k)=0;
                      end
                  end
                  k = k + 1;
              end
          end
          j = j+1;
      end
      remainingPoints(i) = sum(pointAccessible);
      i = i +1;
  end

Variable Data Types:

  numAtoms = 855
  numPoints = 500
  p = 1.4
  atomRadius = <855 * 1 double>
  pointAccessible = <500 * 1 double>
  atomX, atomY, atomZ = <1 * 855 cell>
  sphereX, sphereY, sphereZ = <500 * 1 double>
  remainingPoints = <855 * 1 double>
  • 1
    try to vectorize it, see how to approach it here: http://stackoverflow.com/questions/17540681/improving-matlab-matrix-construction-code-or-code-vectorization-for-begginers/17585725#17585725 – bla Oct 14 '13 at 02:12
  • Thanks, that is new to me. I will try to wrap my head around that. My code is a bit more involved since it includes three while loops; I am trying to figure out whether I can only "vectorize" the inner-loop (and change sphereCoord and neighborCoord to vectors)... –  Oct 14 '13 at 05:05
  • 1
    For a start, you can replace `cell2mat(atomX(i))` by `atomX{i}` - avoiding the overhead of calling cell2mat for every element - and because it's the proper way to access cell-content. – sebastian Oct 14 '13 at 05:33
  • Please remove the references to MacBook from your question, they are completely irrelevant. That the computation takes so long indicates that you need to upgrade your algorithm, not to upgrade your computer. – Bas Swinckels Oct 14 '13 at 08:08

0 Answers0