variables are not updating after each loop in for loop

23 visualizaciones (últimos 30 días)
Lee Lewis
Lee Lewis el 14 de Feb. de 2023
Comentada: Voss el 14 de Feb. de 2023
g1 = [1 1 1 1 1 1 1 1 1 1];
g2 = [1 1 1 1 1 1 1 1 1 1];
%disp(g1);
for i = 1:20
y1 = xor(g1(3),g1(10));
y2 = xor(g2(2),g2(3));
y21=xor(g2(6),y2);
y22=xor(g2(8),y21);
y23=xor(g2(9),y22);
y24=xor(g2(10),y23);
y3 = xor(g2(2),g2(6));
output= xor(g1(10),y3);
disp(output);
%put in other xor commands
g1(1) = y1;
g2(1) = y24;
B = circshift(g1,[0 1]);
C = circshift(g2,[0 1]);
end
disp(g1);
disp(g2);
above is the code i hve written as well as the prompt for the project. I belive I have the code written correctly, because I get the correct answer when I run through the code once. When i try to run the for loop 20 times I get the same answer as if there isn't a for loop. I believe the issue has something to do with my for loop. Id greatly appreciate any help, cheers!

Respuesta aceptada

Voss
Voss el 14 de Feb. de 2023
Well, you define B and C but never use them anywhere, so that could explain why iterations after the first don't matter.
Try it like this (my best guess):
g1 = [1 1 1 1 1 1 1 1 1 1];
g2 = [1 1 1 1 1 1 1 1 1 1];
%disp(g1);
for i = 1:20
y1 = xor(g1(3),g1(10));
y2 = xor(g2(2),g2(3));
y21=xor(g2(6),y2);
y22=xor(g2(8),y21);
y23=xor(g2(9),y22);
y24=xor(g2(10),y23);
y3 = xor(g2(2),g2(6));
output= xor(g1(10),y3);
disp(output);
% g1(1) is y1, and the other elements shift by one, discarding g1(end)
g1 = [y1 g1(1:end-1)];
% g2(1) is y24, and the other elements shift by one, discarding g2(end)
g2 = [y24 g2(1:end-1)];
end
1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 1 0 1 0 0
disp(g1);
0 0 1 1 0 1 1 1 0 0
disp(g2);
1 0 1 1 1 1 0 1 0 1
  2 comentarios
Lee Lewis
Lee Lewis el 14 de Feb. de 2023
Thank you so much for the help. I understand what you're saying. I never ACTUALLY updated g1 and g2, inside of the loop. thanks for the assistence!
Voss
Voss el 14 de Feb. de 2023
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by