how to write a script for multiplication table

I am wondering how to write a script that will print a multiplication table that looks like this: 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25

2 件のコメント

Adam
Adam 2015 年 10 月 28 日
What is the logic behind the sequence? Once you define that the rest is quite simple with
doc sprintf
to print out the numbers in whatever format you choose.
Ellis Fredella
Ellis Fredella 2015 年 10 月 28 日
I need to use a for loop to do it, that's a great shortcut though.

サインインしてコメントする。

回答 (1 件)

Markus Pichler
Markus Pichler 2015 年 10 月 28 日

0 投票

it seems that the sequence contains all the multiples of integers up to the square, i.e., in other form
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
and so forth
it will probably be more efficient with a loop, but you can also try
N = 5;
ix = [1:N].';
v = ix*ix.';
v(ix(:, ones(N, 1))<=ix(:, ones(N, 1)).').'
which throws away nearly half of the elements after generation

1 件のコメント

Ellis Fredella
Ellis Fredella 2015 年 10 月 28 日
yeah, I need to use a loop in order to answer the question in the most efficient way. Thanks though!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2015 年 10 月 28 日

コメント済み:

2015 年 10 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by