Assign one image's index location to another image

I want to assign one image’s index location to another image’s i(current row) and j (current column) location. (I hope that this introduction clarified my situation.)

Also, I need to mention I can only use openCV for I\O functions.

I code these kinds of things in Matlab with the combination of two nested for loop and with one line. For instance:

for R=1:Height
   for  C=1: Width

      % some operations.
   
    outimage(R,C,  :) = processimage(XPrime,YPrime,:); 

end 
end 

I want to learn how can I code this line in C++. I am also using OpenCV.
outimage(R,C,:) = processimage(XPrime,YPrime,:)

To implement this In C++, I read the image like this:

Mat img = imread(imgPath);
    int height = img.rows; 
    int width = img.cols;  

Then, I created for loops :

for (i = 0; i <= height; i++)

    {

        for (j = 0; j <= width; j++)
       {
         

            // some operations


                for (k=0;k<=2;k++) 

            {


        if(Xprime >= 1 && Yprime >= 1 && Xprime <= height && Yprime <= width)
                returnedOutImage[i][j][k] = img[Xprime][Yprime][k];
                //out_image[i][j]=rgb_image[(Xprime-1)*width +Yprime];

                waitKey(0);
            }
       }