Effacer les filtres
Effacer les filtres

Using for loop to evaluate polynomial for different values

1 vue (au cours des 30 derniers jours)
Neha W
Neha W le 24 Sep 2016
Commenté : Image Analyst le 25 Sep 2016
% Here N = 10 ; poly =[5 8 0]
for i = 1:N-1;
A = sum(poly);
B = mod(A,N);
end
i = i + 1;
But the end result shows the value for i = 1 (i.e. A=13, B = 3) only. I need to display A & B for i = 1 to 9

Réponse acceptée

Image Analyst
Image Analyst le 24 Sep 2016
Try this:
N = 10 ;
poly =[5 8 0];
for k = 1:N-1;
A = sum(poly);
B = mod(A,N);
fprintf('At iteration %d, A = %d, and B = %d\n', k, A, B);
end
You'll see
At iteration 1, A = 13, and B = 3
At iteration 2, A = 13, and B = 3
At iteration 3, A = 13, and B = 3
At iteration 4, A = 13, and B = 3
At iteration 5, A = 13, and B = 3
At iteration 6, A = 13, and B = 3
At iteration 7, A = 13, and B = 3
At iteration 8, A = 13, and B = 3
At iteration 9, A = 13, and B = 3
It displays A and B at every one of the 9 iterations, and you can see that they're all the same since A and B never are assigned anything that depends on the iteration number at all.
  2 commentaires
Neha W
Neha W le 25 Sep 2016
Sir, since i = 1: 10
if want A = [0 13 36...477] (% evaluate poly for every iteration till N-1) and the store the mod values in B = [0 3...7] array, What could be the procedure?
Image Analyst
Image Analyst le 25 Sep 2016
You need to index A and B, like A(k) and B(k). Of course since you defined poly as a constant, A and B still won't change.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by