Using for loop to evaluate polynomial for different values

1 次查看(过去 30 天)
% 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

采纳的回答

Image Analyst
Image Analyst 2016-9-24
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 个评论
Neha W
Neha W 2016-9-25
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 2016-9-25
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by