フィルターのクリア

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 Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by