title: How can I swap a section of a row with another within an array?
body: <p>I am in the process of coding a simple <a href="http://www.obitko.com/tutorials/genetic-algorithms/" rel="nofollow">Genetic Algorithm</a> (GA). There are probably countless areas where I have unnecessarily used a <a href="http://www.mathworks.co.uk/help/matlab/ref/for.html" rel="nofollow">for</a> loop. I would like some tips on how to be more MATLAB efficient as well as an answer to my question. As far as I can tell I have succeeded but I am not sure. The area which this code defines is single-point <a href="http://www.obitko.com/tutorials/genetic-algorithms/crossover-mutation.php" rel="nofollow">crossover</a></p>
  
  <p>Here is what I have tried...</p>
  
  <pre><code>crossPoints=randi([1 24],popSize/2,1);
  
  for popNo=2:2:popSize
      isolate=chromoParent(popNo-1:popNo,crossPoints(popNo/2,1)+1:end);
      isolate([1 2],:)=isolate([2 1],:);
      chromoParent(popNo-1:popNo,crossPoints(popNo/2,1)+1:end)=isolate;
  end
  
  chromoChild=chromoParent;
  </code></pre>
  
  <ul>
  <li>where, 'crossPoints' is the point at which single point crossover
  between two binary encoded <a href="http://www.obitko.com/tutorials/genetic-algorithms/encoding.php" rel="nofollow">chromosomes</a> is required.</li>
  <li>'popSize' is the size of the <a href="http://www.obitko.com/tutorials/genetic-algorithms/ga-basic-description.php" rel="nofollow">population</a>, required by my code to
  be an even number</li>
  <li>'isolate' defines the sections of 2 rows which are required to be swapped
  with each other</li>
  <li>'chromoParent' is the initial population which is required to be
  changed by single-point crossover</li>
  <li>'chromoChild' is the resulting population</li>
  <li>Both 'chromoParent' and 'chromoChild' are represented by an array of
  size, popSize x 25 binary characters</li>
  </ul>
  
  <p>Can you spot an error in the way I am thinking about this problem? What's the most efficient way (in computational time) to achieve the same thing? It would help if you could be as broad as possible so that I could begin applying the principles I learn here to the rest of my code.</p>
  
  <p>Thank you.</p>