title: MATLAB: How can I properly work with index values in a meshgrid?
body: <p>Let's say I have two vectors ( Elem and Dren ). Each row in Dren represents a sum of a possible combination of elements from Elem . ID is an array that has the same number of rows as Dren and stores the indexes of Elem that correspond to every row in Dren .</p>
  
  <p>Example:</p>
  
  <pre><code>Elem = [3 2 6];
  Dren = [3; 2; 6; 5; 9; 8; 11];
  ID = [  1     0     0;
      2     0     0;
      3     0     0;
      1     2     0;
      1     3     0;
      2     3     0;
      1     2     3];
  </code></pre>
  
  <p>Then I used a meshgrid for Elem and Dren to get all possible pairs of these values and created a new array ( R ) with their ratios, as shown below:</p>
  
  <pre><code>[E,D]=meshgrid(Elem,Dren);
  for line=1:size(E,1)
     for col=1:size(E,2)
        R(line,col)=D(line,col)./E(line,col);
     end
  end
  </code></pre>
  
  <p>This gives me:</p>
  
  <pre><code>R =
  1.0000e+000  1.5000e+000  5.0000e-001
  6.6667e-001  1.0000e+000  3.3333e-001
  2.0000e+000  3.0000e+000  1.0000e+000
  1.6667e+000  2.5000e+000  8.3333e-001
  3.0000e+000  4.5000e+000  1.5000e+000
  2.6667e+000  4.0000e+000  1.3333e+000
  3.6667e+000  5.5000e+000  1.8333e+000
  </code></pre>
  
  <p>I have filtered the results of R and selected the ones that are lower than 6, so I get:</p>
  
  <pre><code>R6 = R(find(R&lt;6))
  
  R6 =
  1.0000e+000
  6.6667e-001
  2.0000e+000
  1.6667e+000
  3.0000e+000
  2.6667e+000
  3.6667e+000
  1.5000e+000
  1.0000e+000
  3.0000e+000
  2.5000e+000
  4.5000e+000
  4.0000e+000
  5.5000e+000
  5.0000e-001
  3.3333e-001
  1.0000e+000
  8.3333e-001
  1.5000e+000
  1.3333e+000
  1.8333e+000
  </code></pre>
  
  <p>Now I would like to create a new array that will store these filtered values in the first column and show the row values from ID equivalent to them. In other words, the first column would be R(find(R&lt;6) and the other columns would be the Elem indexes related to the Dren value used in every R calculation. Since I used the find function in R, I don't know how to associate the correct ID rows.</p>
  
  <p>For example, for R6(1,1)= 1.0000e+000, we know it was calculated from D(1,1)=3, so the corresponding ID row is [ 1     0     0]. The first row of the array would therefore be:</p>
  
  <pre><code>Store(1,:) = [ 1.0000e+000 1     0     0 ]
  </code></pre>
  
  <p>How can I do this for all the elements in R6?</p>