variables are not updating after each loop in for loop

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!

 Réponse acceptée

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 commentaires

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!
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Version

R2022a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by