Matlab - Matrix
Matlab - Matrix
MATLAB - Matrix
A matrix is a two-dimensional array of numbers.
In MATLAB, you create a matrix by entering elements in each row as comma or space delimited
numbers and using semicolons to mark the end of each row.
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]
Live Demo
MATLAB will execute the above statement and return the following result −
a=
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
mx(m, n);
For example, to refer to the element in the 2nd row and 5th column, of the matrix a, as created in the
last section, we type −
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
Live Demo
a(2,5)
MATLAB will execute the above statement and return the following result −
ans = 6 x
Consulting + Software
VFF - Specialist for random packing and column
To reference all the elementsinternals th column we type A(:,m).
in the m"Made in Germany" for decades.
Vereinigte Füllkörper… Open
Page 2 of 5
Let us create a column vector v, from the elements of the 4th row of the matrix a −
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
Live Demo
v = a(:,4)
MATLAB will execute the above statement and return the following result −
v=
4
5
6
7
You can also select the elements in the mth through nth columns, for this we write −
a(:,m:n)
Let us create a smaller matrix taking the elements from the second and third columns −
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
Live Demo
a(:, 2:3)
MATLAB will execute the above statement and return the following result −
ans =
2 3
3 4
4 5
5 6
In the same way, you can create a sub-matrix taking a sub-part of a matrix.
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
x
Live Demo
a(:, 2:3)
MATLAB will execute the above statement and return the following result −
Page 3 of 5
ans =
2 3
3 4
4 5
5 6
In the same way, you can create a sub-matrix taking a sub-part of a matrix.
3 4 5
4 5 6
To do this, write −
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
Live Demo
sa = a(2:3,2:4)
MATLAB will execute the above statement and return the following result −
sa =
3 4 5
4 5 6
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
Live Demo
a( 4 , : ) = []
MATLAB will execute the above statement and return the following result − x
a=
1 2 3 4 5
Page 4 of 5
2 3 4 5 6
3 4 5 6 7
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
Live Demo
a(: , 5)=[]
MATLAB will execute the above statement and return the following result −
a=
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
Example
In this example, let us create a 3-by-3 matrix m, then we will copy the second and third rows of this
matrix twice to create a 4-by-3 matrix.
a = [ 1 2 3 ; 4 5 6; 7 8 9];
Live Demo
new_mat = a([2,3,2,3],:)
new_mat =
4 5 6
7 8 9
4 5 6
7 8 9
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified
x
expert to boost your career.
Matrix Operations
In this section, let us discuss the following basic and commonly used matrix operations −
Page 5 of 5
Division of Matrices
Transpose of a Matrix
Concatenating Matrices
Matrix Multiplication
Determinant of a Matrix
Inverse of a Matrix