How to combine 3 for loops into one?
3 views (last 30 days)
Show older comments
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,:)
0 Comments
Accepted Answer
Star Strider
on 8 Feb 2020
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
Star Strider
on 8 Feb 2020
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.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!