필터 지우기
필터 지우기

Get rid of unwanted output

조회 수: 1 (최근 30일)
Krish Desai
Krish Desai 2015년 12월 10일
편집: James Tursa 2015년 12월 10일
I have the following code
function output=beautyofmath(i)
for i = 1:9
if i == 1
j(i, 1) = i;
else
j(i, 1) = j(i - 1, 1) * 10 + i;
end
j(i, 2) = i;
j(i, 3) = j(i, 1) * 8 + j(i, 2);
fprintf('%d x 8 + %d = %d\n', j(i, 1), j(i, 2), j(i, 3));
end
The problem is it outputs the following no matter the i value. If I type in beautyofmath(5), I only want the first 5 values to show up. How do I fix this?
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321

채택된 답변

James Tursa
James Tursa 2015년 12월 10일
편집: James Tursa 2015년 12월 10일
You overwrite your input variable i with your for loop index i, which always goes up to 9. So do this instead:
function output=beautyofmath(i_max)
for i = 1:i_max

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by