0
\$\begingroup\$

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 < _roiY1; i++)
{
    for (int j = 0; j < newWidth; j++)
    {
        channel_gr[i*newWidth + j] = clipLevel;
    }
}

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

//Gr(:, 1:ROIX(1))      = clipLevel;
for (int i = 0; i < newHeight; i++)
{
    for (int j = 0; j < _roiX1; j++)
    {
        channel_gr[i*newWidth + j] = clipLevel;
    }
}
//Gr(:, ROIX(2)+1:end)  = clipLevel;
for (int i = 0; i < newHeight; i++)
{
    for (int j = _roiX2-1; j < newWidth; j++)
    {
        channel_gr[i*newWidth + j] = clipLevel;
    }
}
\$\endgroup\$
1
\$\begingroup\$

The block

for (int i = 0; i < _roiY1; i++)
{
    for (int j = 0; j < 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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.