필터 지우기
필터 지우기

Using for loop to evaluate polynomial for different values

조회 수: 1 (최근 30일)
Neha W
Neha W 2016년 9월 24일
댓글: Image Analyst 2016년 9월 25일
% 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