Skip to main content

Creating Multiplication tables using Matlab

Program

Write a program to generate Multiplication tables of a given number using Matlab. Also generate Multiplication table for all numbers at once, like the one below:
Multiplication Table using Matlab
Multiplication Table 20 x 20
To know more about the multiplication tables you can refer:

Solution

Program 1: We will ask the user to give input of a number. Then we will use a for loop to print out the multiplication table of the corresponding number. 
Program 2: We will write a program using functions.
Program 3: We will create 10x10 multiplication table using the solution given here: Stack Overflow. Program 4: Generate 10x10 multiplication table using the nested for loops.
You can refer to the explanation section for better understanding of the program. Some of the concepts you need to know to solve this problem are as follows:
1) input function in Matlab
2) for loop in Matlab - This website has a very good explanation of for loops
3) fprintf 
4) function in Matlab
5) colon operator, Matlab colon operator
6) Transpose of a matrix in matlab, without using the inbuilt transpose
7) nested for loops

Program 1

1
2
3
4
5
number = input('Enter the number you want the multiplication table to be created');

for i = [1:20]
    fprintf('%d x %d = %d\n',number,i,number*i);
end
I have saved this file as multiplication_table.m on my local computer.

Explanation 1

Line 1: We are asking the user to enter the number for which the user wants the multiplication table to be created.
Line 3: We are starting the for loop. Lets understand the for loop by taking an example. Lets say the user has entered the number as 5. Now in the first iteration the value of i is 1. Now the fprintf gets executed. In fprintf we see that there are three parameters - number, the value of i in the current iteration, the value of i with the number. In this first iteration the three values will be 5, 1, 5 respectively. Now the second iteration starts and the values will be 5, 2, 10 respectively. In the similar way all the other values upto 20 gets executed and you will get a nice table.

Program 2


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function generate_table(number)
%this function will generate a multiplication table
%of the input parameter.
%Documentation
%generate_table(x)

for i = [1:20]
    fprintf('%d x %d = %d\n',number,i,number*i);
end
end
I have saved this program as generate_table.m on my local computer.

Explanation 2

This is similar to the above program but here the user will enter the number for which he wants the Multiplication table to be generated as a parameter instead of asking the user using the input function.

Program 3


>>(1:10)' * (1:10)

Explanation 3

I have typed this program in my command prompt and got the desired output. Here we are multiplying two matrices. The first matrix is 1:10 a row matrix which we are using the transpose to create column matrix. And the second one is row matrix. Now we know that if we are multiplying a column vector and a row vector we get a matrix. Here we will get a 10x10 matrix which is like the above one.

Program 4

1
2
3
4
5
6
for i = (1:10)
    for j = (1:10)
        fprintf('%d\t',i*j);
    end
    fprintf('\n');
end
I have saved this file as table_multiplication.m on my local computer.

Explanation 4

Let us understand the above code by looking at each iteration. Now first consider the outer for loop. During the first iteration the value of i becomes equal to 1. As i has got a value the for loop will go inside the loop. Now the second for loop or inside for loop gets executed and j gets a value 1. In the similar way j gets values till 10 and the fprintf in this loop prints(i*j = 1*1(For first iteration of both the loops)) like this:
1    2    3    4    5    6    7    8    9    10
A large gap between the numbers is because of the '\t' which is a escape sequence telling the matlab to give a gap of tab space(4 non braking spaces). As we have reached 10 the condition of the second for loop is false and this loop gets exited giving back the control to the first loop. A fprintf function is invoked to create a line break so that we get the next set of values or the 2 multiplication table printed. In the similar way the 3 multiplication table is also printed. After all the iterations are completed i.e. i = 10 and j = 10 reaches then we can observe the table given in the image.

Try it yourself

1) Write your version of program such that you will ask the user two numbers in which the first number is the number for which the user wants the multiplication table and the second number will be the number of iterations. Comment your program in the comment box below.

Input/Output

Multiplication table generation using matlab
  Multiplication table generation using matlab
Multiplication table generation using Matlab

Final Note

As always thanks for stopping by and if you have any doubt or suggestion or didn't understand anything then please do contact me or comment below in the comment box. I will be really happy to help you. You can contact me from here: contact me
The above program was high lighted using hilite.me.
keywords: multiplication table, transpose, 

Popular posts from this blog

Making a quiz web app with python and flask

Edit : When you are creating a web app with h tml templates, then y ou will have to sa ve the html file in templates folder in the Current Wor ki ng Directory( CWD). If you save the file in the C W D directl y you will get a TemplateNotFound error. Thank you Udhay for pointing it out.   In this post we will create a quiz website using python . I will be using the flask framework . After reading this tutorial you will learn form submission , flask templates , python code in flask templates , shuffling the questions and options with the random module and few others.  Please note that this tutorial is not big as it seems to be. Some of the code has been rewritten to maintain consistency and also font size is somewhat big so that your eyes won't get stressed reading this tutorial. Also the content has not occupied the full width of the page. In this tutorial I am assuming that you are having a very basic understanding of the flask framework . Please refer the documentation

Problem 11 Project Euler Solution with python

Largest product in a grid In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07

Add/Embed SVG to Blogger website

In this post I will tell you my method(trick) of adding SVG images in a blogger website or blog. Before starting , the first thin g I am assu m ing is that you are aware of SVG if you are here. If not please see S calable V ec tor G raphics Recently when I tried to embed a SVG image for a post on pygal, I tried uploading the SVG file and blogger Image uploader came up with an error, because of which I had to find some other way.  SVG File upload Error in Blogger  I started sea rc hing Google " Embed SVG in Blogger " . I found blogorrhea , w h ich gave some i nformatio n on add ing SVG directly as a markup , which worked , but I faced another problem using this . Also th is guy has used lot of Javascript which was confusin g for me, being new to using SVG.   So I first t houg ht of learning on h ow to embed SVG in HTML and t his on e worked out. Actually we can embed SVG in HTML i n following ways: Using Object tag Using Iframe tag Using embed tag Us