Implementation of MATLAB's `im2col`
This is a valid C code which implements the above:

void ImageToColumns(float* mO, float* mI, int numRows, int numCols, int blockRadius)
{
    int blockSize, blockNumElements, colImageColIdx, colImageRowIdx;
    int ii, jj, kk, ll;

    blockSize           = (2 * blockRadius) + 1;
    blockNumElements    = blockSize * blockSize;
    colImageRowIdx      = -1;

    for (ii = blockRadius; ii &lt; (numRows - blockRadius); ii++)
    {
        for (jj = blockRadius; jj &lt; (numCols - blockRadius); jj++)
        {
            colImageRowIdx = colImageRowIdx + 1;
            colImageColIdx = -1;

            for (kk = -blockRadius; kk &lt;= blockRadius; kk++)
            {
                for (ll = -blockRadius; ll &lt;= blockRadius; ll++)
                {
                    colImageColIdx = colImageColIdx + 1;
                    mO[(colImageRowIdx * blockNumElements) + colImageColIdx] = mI[((ii + kk) * numCols) + jj + ll];
                }

            }
        }
    }


}


Clearly this a memory bounded algorithm.