Get rid of unwanted output

2 次查看(过去 30 天)
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 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by