Matlab to C++ percentile clipping I have a 2D matrix in Matlab and I want to convert the Matlab code into C++.

In C++ I have int* channel_gr. The commented code is the original Matlab code.

Is there a faster way to execute this code?

//Gr(1:ROIY(1), :)      = clipLevel;
for (int i = 0; i &lt; _roiY1; i++)
{
    for (int j = 0; j &lt; newWidth; j++)
    {
        channel_gr[i*newWidth + j] = clipLevel;
    }
}

//Gr(ROIY(2)+1:end, :)  = clipLevel;
for (int i = _roiY2-1; i &lt; newHeight; i++)
{
    for (int j = 0; j &lt; newWidth; j++)
    {
        channel_gr[i*newWidth + j] = clipLevel;
    }
}

//Gr(:, 1:ROIX(1))      = clipLevel;
for (int i = 0; i &lt; newHeight; i++)
{
    for (int j = 0; j &lt; _roiX1; j++)
    {
        channel_gr[i*newWidth + j] = clipLevel;
    }
}
//Gr(:, ROIX(2)+1:end)  = clipLevel;
for (int i = 0; i &lt; newHeight; i++)
{
    for (int j = _roiX2-1; j &lt; newWidth; j++)
    {
        channel_gr[i*newWidth + j] = clipLevel;
    }
}
The block

for (int i = 0; i &lt; _roiY1; i++)
{
    for (int j = 0; j &lt; newWidth; j++)
    {
        channel_gr[i*newWidth + j] = clipLevel;
    }
}


can be made more concise by using std::fill.

std::fill(channel_gr, channel_gr+_roiY1*newWidth, cliplevel);


I won't vouch for better performance though.