Borrar filtros
Borrar filtros

Using for loop to evaluate polynomial for different values

1 visualización (últimos 30 días)
Neha W
Neha W el 24 de Sept. de 2016
Comentada: Image Analyst el 25 de Sept. de 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

Respuesta aceptada

Image Analyst
Image Analyst el 24 de Sept. de 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 comentarios
Neha W
Neha W el 25 de Sept. de 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 el 25 de Sept. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by