<p>I think this must be simple but I can't get it right...</p>

<p>I have an MxM triangular matrix, the coefficients of which are stored in a vector, row by row. 
For example:</p>

<pre><code>M =   [ m00 m01 m02 m03 ] 
      [     m11 m12 m13 ]
      [         m22 m23 ]
      [             m33 ]
</code></pre>

<p>is stored as </p>

<pre><code>coef[ m00 m01 m02 m03 m11 m12 m13 m22 m23 m33 ]
</code></pre>

<p>Now I'm looking for a non-recursive algorithm that gives me for matrix size <code>M</code> and coefficient array index <code>i</code></p>

<pre><code>unsigned int row_index(i,M)
</code></pre>

<p>and </p>

<pre><code>unsigned int column_index(i,M)
</code></pre>

<p>of the matrix element that it refers to. So,
<code>row_index(9,4) == 3</code>, <code>column_index(7,4) == 2</code> etc. if the index counting is zero-based.</p>

<p>EDIT: Several replies using an iteration have been given. Does anyone know of algebraic expressions?</p>

Took me some time to understand what you needed! :)
```
```
unsigned int row_index(int i, int m)
{
    int iCurrentRow = 0;
    int iTotalItems = 0;
    for(int j = m; j > 0; j--)
    {
        iTotalItems += j;

        if( (i+1) <= iTotalItems)
            return iCurrentRow;

        iCurrentRow ++;
    }

    return -1; // Not checking if "i" can be in a MxM matrix.
}
```
```
Sorry forgot the other function.....
```
```
unsigned int column_index(int i, int m)
{
    int iTotalItems = 0;
    for(int j = m; j > 0; j--)
    {
        iTotalItems += j;

        if( (i+1) <= iTotalItems)
            return m - (iTotalItems - i);
    }

    return -1; // Not checking if "i" can be in a MxM matrix.
}
```
```