title: How to speed up this MATLAB code in the loop?
body: <p>I have a matrix with 1200000 rows and 18 columns. It is traffic data and each row is unique and the first column is the "vehicle ID", the second is the "frame ID" and the 16th column shows the "front vehicle ID". I want MATLAB to find the row vector of the front vehicle from the whole data in the same frame ID and place it in another matrix called PV. Also if there is no car in front, place zero vector. The whole matrix is called "H1". I used the code below and for the work I have used percentage. However, its runtime is too slow and takes more than 14 hours on a 16GB memory. It is too long for me since I have 10 other such data. Please help me to make it faster and better.</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>for i=1:1200000
      i*100/m           % Shows what percent of the work done
      g = H1(H1(:,1)==H1(i,16),:);          
      g = g(g(:,2)==H1(i,2),:);
      if isempty(g)==1
          PV(i,:) = zero(1,:);
      else
          PV(i,[1:17])=g(1,[1:17]);
      end
  end
  </code></pre>
  
  <p><strong>EDIT:</strong> The data is like a book with 10000 pages. Each page is a frame ID (the page number is the frame ID) which has many cars in it with unique vehicle ID. So any page shows an image which is took from above and there are many cars inside this image. If we attach the images together with a time interval of 0.1 second we get the driving film of the vehicles. This data includes the x and y coordinates of the vehicles which each frame can be depicted using the "plot" command in MATLAB. This data also includes the preceding vehicle (the vehicle in front of the subject vehicle) with its ID in the 16th column in each row. It is worth note that the information of all vehicles are present in the data. If there is no vehicle in front of the subject vehicle the number in the 16th column is zero. So any row shows the information of only one vehicle. The data are sorted with respect to frame ID.</p>
  
  <p>Now I need to extract the row of the preceding vehicle from the whole matrix and place it in the matrix of <strong>PV</strong>. The problem is that the percentage goes slowly when it gets to about 5%. Here is a sample of the data:</p>
  
  <pre><code>[629 2033...581]
  </code></pre>
  
  <p>the first column is the vehicle ID, the second is the frame ID and the 16th is the preceding vehicle ID in this frame ID. Here the car number 581 is in front of the car number 629 in the frame ID of 2033. Now I need to extract the data for the vehicle ID 581 in the frame 2033 and place it in the PV matrix.</p>
  
  <p>More samples: the first is the vehicle ID, the second is the frame ID and the third number is the preceding vehicle ID.</p>
  
  <pre><code>[629    2033    688 1113433338200   28.703  462.09  6042802.932 2133529.776 56.3    7.9 3   12.8    5.09    3   581 640 95.39]
  [577    2033    465 1113433338200   79.392  618.232 6042833.946 2133691.06  17.3    8.4 2   30.19   -0.37   7   0   3362    0]
  [580    2033    621 1113433338200   53.4    542.455 6042817.601 2133612.708 18.3    7.5 2   20.49   -0.09   5   572 3361    80.9]
  [581    2033    565 1113433338200   27.252  557.481 6042789.779 2133624.359 16.8    7.4 2   21.25   4.19    3   573 629 62.54]
  </code></pre>
  
  <p>Sorry for the long explanation and thanks for your help in advance.</p>