MATLAB Tutorials Class 2: Creation of Matrix and array in MATLAB
MATLAB Tutorials Class 2: Creation of Matrix and array in
MATLAB
Hai... We addressed the environmental setup and layout configuration in my previous post. Here we going to see Matrix and array. The expansion of MATLAB is the 'Matrix laboratory'. MATLAB script operations are based on matrix and array.
1. What is the Matrix?
The matrix is a framework for the number of columns and rows. The matrix may look rectangular or square. A count of rows and columns is used for matrix status representation ( no. of. row X no. of. column matrix). For instance, if the number of rows and columns is 3, the matrix is known as the 3 x 3 matrix.
2. What is an array?
An array is an arrangement of elements together in a single row.
3. How to create an array in MATLAB?
To do this, you can use the command window. For execution, after entering the code line, give enter. If you want to see the answer in the same command window means, you don't put the semicolon at last. Eventually, if you give a semicolon, you'll see the result in the workspace. Good practice putting a semicolon.
Array Creation in MATLAB:
Method 1: To create an array, give space or comma between each number as shown below
>> array_creation=[1 2 3 4 5 6 7]
array_creation =
1 2 3 4 5 6 7
>> array_creation=[1,2,3,4,5,6,7]
array_creation =
1 2 3 4 5 6 7
Method 2: It's another way to create an array. Its general syntax is 'first number: difference: last number.' If you did not mention the difference means, it will consider 1.
>> array_creation=1:10
array_creation =
1 2 3 4 5 6 7 8 9 10
>> array_creation=1:2:10
array_creation =
1 3 5 7 9
Matrix creation in MATLAB:
Separate rows with semicolons to create a matrix that has multiple rows.
>> matrix_creation=[1;2;3;4]
matrix_creation =
1
2
3
4
>> matrix_creation=[1,2;3,4]
matrix_creation =
1 2
3 4
>> matrix_creation=[1,2,3;4,5,6;7,8,9]
matrix_creation =
1 2 3
4 5 6
7 8 9
How to create a zero's matrix and one's matrix in MATLAB?
Zero Matrix :
Creates a matrix that contains zeros in full. Syntax is
zero_matrix=zeros()
case 1:if we give syntax like 'zero' means it creates 1 x 1 zero matrices.
>> zero_mat_creation=zeros
zero_mat_creation =
0
case 2: If we mention a number, that number is considered to be a row and column count.
>> zero_mat_creation=zeros(2)
zero_mat_creation =
0 0
0 0
>> zero_mat_creation=zeros(5)
zero_mat_creation =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
case 3: if suppose we need a specified number of row and column zeros matrix means we can mention the number of row and column as given below
zero_mat_creation=zeros(2,3)
zero_mat_creation =
0 0 0
0 0 0
>> zero_mat_creation=zeros(5,8)
zero_mat_creation =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Ones Matrix :
Creates a matrix that contains ones in full. Syntax is
zero_matrix=ones()
case 1:if we give syntax like 'ones' means it creates 1 x 1 ones matrices.
>> one_mat_creation=ones
one_mat_creation =
1
case 2: If we mention a number, that number is considered to be a row and column count.
>> one_mat_creation=ones(2)
one_mat_creation =
1 1
1 1
>> one_mat_creation=ones(4)
one_mat_creation =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
case 3: if suppose we need a specified number of row and column ones matrix means we can mention the number of row and column as given below
>> one_mat_creation=ones(5,2)
one_mat_creation =
1 1
1 1
1 1
1 1
1 1
>> one_mat_creation=ones(5,9)
one_mat_creation =
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
Identity Matrix:
A square matrix with 1s on the main diagonal, and 0s everywhere else is an identity matrix. Syntax is
>> identity_mat_creation=eye
identity_mat_creation =
1
>> identity_mat_creation=eye(5)
identity_mat_creation =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
>> identity_mat_creation=eye(3)
identity_mat_creation =
1 0 0
0 1 0
0 0 1
Note: while creating zeros, ones, and identity matrix will be in type (or class) of double. we can get another class by mentioning them. Syntax is
zeros(5,'typename')
The typename are 'double', 'single', logical, 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', or 'uint64'.
>> zero_mat_creation=zeros(5,8,'uint8')
zero_mat_creation =
5×8 uint8 matrix
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
>> one_mat_creation=ones(5,9,'logical')
one_mat_creation =
5×9 logical array
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
>> identity_mat_creation=eye(5,6,'int32')
identity_mat_creation =
5×6 int32 matrix
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
Reference:
Thanks to MATLAB Documentation
We have discussed the matrix and array creation in Matlab so far. We will see how to generate random numbers in MATLAB.
Thank you so much😇
Please subscribe😊.
Share with your friends and please provide your feedback about this post🙏🙏.
Comments
Post a Comment