How to combine 3 for loops into one?

I have the following code, which I would like to create a loop for but I have 3 variables that I would like to change.
I have made a loop with one variable where i change the columns but not sure how I can do it like this
The code below is just an example but I need to do this for a much larger range of about 50 for my image.
I(54:69,16,:)= I(39:54,16,:)
I(69:84,16,:)= I(84:99,16,:)
I(55:70,17,:)= I(40:55,17,:)
I(70:85,17,:)= I(85:100,17,:)
I(56:71,18,:)= I(41:56,18,:)
I(71:86,18,:)= I(86:101,18,:)

 Accepted Answer

Try this:
Rv = [54:56; 69:71; 39:41; 84:86]; % Row Start Matrix
Cv = 16:18; % Column Vector
Rc = 0:15; % Row Subscript Address Length
for k = 1:3
% Q1 = [Rv(1,k)+Rc; Rv(3,k)+Rc] % Check Addressing (Delete)
% Q2 = [Rv(2,k)+Rc; Rv(4,k)+Rc] % Check Addressing (Delete)
I(Rv(1,k)+Rc,Cv(k)) = I(Rv(3,k)+Rc, Cv(k));
I(Rv(2,k)+Rc,Cv(k)) = I(Rv(4,k)+Rc, Cv(k));
end
This works for the code you posted. You will need to expand the ‘Rv’ and ‘Cv’ vectors for the rest of your image. (The ‘Q1’ and ‘Q2’ variables make the addressing visible when you un-comment them. Delete them when you no longer need them.)

4 Comments

Thanks, this works great, however my image column and row pixels are not exact and not sure how to correct this?
Rv = [54:154; 69:169; 39:139; 84:184]; % Row Start Matrix
Cv = 16:116; % Column Vector
Rc = 0:15; % Row Subscript Address Length
for k = 1:100
% Q1 = [Rv(1,k)+Rc; Rv(3,k)+Rc] % Check Addressing (Delete)
% Q2 = [Rv(2,k)+Rc; Rv(4,k)+Rc] % Check Addressing (Delete)
I(Rv(1,k)+Rc,Cv(k)) = I(Rv(3,k)+Rc, Cv(k));
I(Rv(2,k)+Rc,Cv(k)) = I(Rv(4,k)+Rc, Cv(k));
end
imshow(I)
From the image if you can see, I am trying to replace the black metal artifact with the values from the surrounding but i think maybe if I use the same row matrix for a few columns by including another loop this would work.
Screenshot 2020-02-08 at 15.53.53.png
As always, my pleasure!
My intent was to provide a solution to the problem you asked about, that being a loop to replace the code you posted.
It would certainly be appropriate to add another loop, if that is what you need to do, however there may be more efficient methods to do what you want in the Image Processing Toolbox functions.
You may want to interpolate the values of the background over the artifact. That may require segmenting it, then eliminating it, and doing the appropriate interpolation or extrapolation to fill that area with the background.
Image processing is not an area of my expertise, so I am not certain how to advise you further.
Thanks but I think for now I will use this method and with time I will try to try the method you suggested.
Do you know how I could create a separate loop for the column vector? I have tried but have not got anywhere yet.
Try this:
for k1 = 1:3
for k2 = 1:numel(Cv)
I(Rv(1,k1)+Rc,Cv(k2)) = I(Rv(3,k1)+Rc, Cv(k2));
I(Rv(2,k1)+Rc,Cv(k2)) = I(Rv(4,k1)+Rc, Cv(k2));
end
end
I am not certain what you want to do. That should work.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!