How to combine 3 for loops into one?

4 visualizaciones (últimos 30 días)
Anon
Anon el 8 de Feb. de 2020
Comentada: Star Strider el 8 de Feb. de 2020
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,:)

Respuesta aceptada

Star Strider
Star Strider el 8 de Feb. de 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 comentarios
Anon
Anon el 8 de Feb. de 2020
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.
Star Strider
Star Strider el 8 de Feb. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Geometric Transformation and Image Registration en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by