title: Translate a 2D coordinate into a 1D array
body: <p>I have also read:</p>
  
  <p><a href="https://stackoverflow.com/questions/1730961/convert-a-2d-array-index-into-a-1d-index">Convert a 2D array index into a 1D index</a></p>
  
  <p>I am trying to debug my program and I would need to understand how could we convert a 2d coordinate into a 1d coordinate.</p>
  
  <p>If I have an IJ coordinate, for example: (3284,1352) and we want to access an unidimensional array which is (3492,2188), how could we achieve that?</p>
  
  <p>Until now I have tried:</p>
  
  <p>1) I thought that you offset for each row and inside each row for each column as:</p>
  
  <pre><code>i x j = 3284 * 1352 = 4.439.968
  </code></pre>
  
  <p>If we have that the coordinate (3284,1352) graphically corresponds to the segment which has a gray level intensity of 1:</p>
  
  <p><a href="https://i.stack.imgur.com/eufRJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eufRJ.png" alt="enter image description here"></a></p>
  
  <p>And we try to access the unidimensional array which has for each pixel its gray value, in pixel: <code>4.439.968</code> we have:</p>
  
  <p><a href="https://i.stack.imgur.com/9Auoh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9Auoh.png" alt="enter image description here"></a></p>
  
  <p>A gray intensity of <code>14</code></p>
  
  <p>Which is quite strange because of:</p>
  
  <p>-> Segment number 14 is not being displayed by ITKSnap:</p>
  
  <p><a href="https://i.stack.imgur.com/cWDy1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cWDy1.png" alt="enter image description here"></a></p>
  
  <p>-> Our segment has a gray level of 1, so then we should after manually calculating which index to access, find that gray level:</p>
  
  <p><a href="https://i.stack.imgur.com/nTbfz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nTbfz.png" alt="enter image description here"></a></p>
  
  <p>2) The second way I have tried is to calculate it as:</p>
  
  <pre><code>column clicked * number of total rows + row clicked;
  
  j * xLength + i;
  </code></pre>
  
  <p>In our example is:</p>
  
  <pre><code>1352 * 3492 + 3284 = 4.724.468
  </code></pre>
  
  <p>If we try to find that pixel in the data:</p>
  
  <p><a href="https://i.stack.imgur.com/SLimE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SLimE.png" alt="enter image description here"></a></p>
  
  <p>We find that the gray level is 0, which corresponds to the background.</p>
  
  <p>What is wrong with the conversion?</p>
  
  <p>Could you help me please?</p>