English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A matrix is a two-dimensional array of numbers.
In MATLAB, you can create a matrix by entering elements separated by commas or spaces in each row and using a semicolon to mark the end of each row.
For example, let us create a4×5matrixone-
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]
MATLAB will execute the above statements and return the following results-
a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8
To refer to a matrixmxthem rowtheWe write the element in the n columns of-
mx(m, n);
For example, refers to the component2inthe secondrow and5tocolumn, theoneAs created in the previous section, we input-
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]); a(2,5)
MATLAB will execute the above statements and return the following results-
ans = 6
To refer to all elements at mtocolumn, we have type A (:, meter).
Let us create a column vector v, from4elementstorows of matrix A-
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]); v = a(:,4)
MATLAB will execute the above statements and return the following results-
v = 4 5 6 7
You can also choose the m-th elementtimestimestocolumns, for this we write-
a(:,m:n)
Let's create a smaller matrix to absorb 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]); a(:, 2:3)
MATLAB will execute the above statements and return the following results-
ans = 2 3 3 4 4 5 5 6
In the same way, you can create a submatrix that takes a subpart of the matrix.
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]); a(:, 2:3)
MATLAB will execute the above statements and return the following results-
ans = 2 3 3 4 4 5 5 6
In the same way, you can create a submatrix that takes a subpart of the matrix.
For example, let's create a submatrixsa, which contains-the internal subpart.
3 4 5 4 5 6
To do this, please write-
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]); sa = a(2:3,2:4)
MATLAB will execute the above statements and return the following results-
sa = 3 4 5 4 5 6
You can delete an entire row or column of a matrix by assigning an empty array [] to that row or column. Essentially, [] represents an empty array.
For example, let's delete-the fourth row
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]); a( 4 , : ) = []
MATLAB will execute the above statements and return the following results-
a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7
Next, let's delete-the fifth column
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]); a(: , 5)=[]
MATLAB will execute the above statements and return the following results-
a = 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7
In this example, let's create a3×3the matrix m, and then copy the second and third rows twice to create4×3Matrix.
Use the following code to create a script file-
a = [ 1 2 3 ; 4 5 6; 7 8 9]); new_mat = a([2,3,2,3],:)
When the file is executed, it displays the following result-
new_mat = 4 5 6 7 8 9 4 5 6 7 8 9
In this section, let's discuss the following basic and commonly used matrix operations-