MATLAB matrix correlation calculation

Addition and subtraction of MATLAB matrix

MATLAB matrix can have addition and subtraction operations, but the matrix of two operands must have the same number of rows and columns.

Detailed examples

Create a script file in MATLAB with the following code:

a = [ 1 2 3 ; 4 5 6; 7 8 9];b = [ 7 5 6 ; 2 0 8; 5 7 1];c = a + bd = a - b

Run the file and display the results:

c =     8     7     9     6     5    14    12    15    10d =    -6    -3    -3     2     5    -2     2     1     8

MATLAB division (left, right) matrix

There are two matrix division symbols in MATLAB: left division "\" and right division "/".

Note: the matrix of these two operands must have the same number of rows and columns.

Detailed examples

Create a script file in MATLAB with the following code:

a = [ 1 2 3 ; 4 5 6; 7 8 9];b = [ 7 5 6 ; 2 0 8; 5 7 1];c = a / bd = a  b

Run the file and display the results:

c =
  -0.52542   0.68644   0.66102  -0.42373   0.94068   1.01695  -0.32203   1.19492   1.37288
d =
  -3.27778  -1.05556  -4.86111  -0.11111   0.11111  -0.27778   3.05556   1.27778   4.30556

MATLAB matrix scalar operation

The scalar operation of MATLAB matrix is to add, subtract, multiply or divide a digital matrix.

The same number of scalar operations added to the rows and columns of each element with the original matrix, subtraction, multiplication or division will produce a new matrix.

Detailed examples

Create a script file in MATLAB with the following code:

a = [ 10 12 23 ; 14 8 6; 27 8 9];b = 2;c = a + bd = a - b
e = a * bf = a / b

Run the file and display the results:

c =    12    14    25    16    10     8    29    10    11d =     8    10    21    12     6     4    25     6     7e =    20    24    46    28    16    12    54    16    18f =    5.0000    6.0000   11.5000    7.0000    4.0000    3.0000   13.5000    4.0000    4.5000

Transpose of MATLAB matrix

The transpose operation of matrix in MATLAB is represented by a single quotation mark ('), which can switch the rows and columns of a matrix.

Detailed examples

Create a script file in MATLAB with the following code:

a = [ 10 12 23 ; 14 8 6; 27 8 9]b = a'

Run the file and display the following results:

​​​​​​​

a =    10    12    23    14     8     6    27     8     9b =    10    14    27    12     8     8    23     6     9

MATLAB series matrix

MATLAB uses a pair of brackets "[]", which can connect the two matrices to create a new matrix.

There are two types of MATLAB series matrix:

  • Horizontal concatenation: the two matrices to be connected are separated by commas "," and ".

  • Vertical concatenation: the two matrices to be connected use the semicolon ";" Separated.

Detailed examples

Create a script file in MATLAB with the following code:

a = [ 10 12 23 ; 14 8 6; 27 8 9]b = [ 12 31 45 ; 8 0 -9; 45 2 11]c = [a, b]d = [a; b]

Run the file and display the results:

​​​​​​​

a =    10    12    23    14     8     6    27     8     9b =    12    31    45     8     0    -9    45     2    11c =    10    12    23    12    31    45    14     8     6     8     0    -9    27     8     9    45     2    11d =    10    12    23    14     8     6    27     8     9    12    31    45     8     0    -9    45     2    11

MATLAB matrix multiplication

If there are two matrices A and B in MATLAB, where a is m*n matrix and B is n*p matrix, then their multiplication can produce an m*n matrix C.

MATLAB matrix multiplication only occurs in matrix multiplication in which the number of columns of matrix A is equal to the number of rows of matrix B, and the second matrix in the corresponding column is multiplied by the elements of the rows in the first matrix.  

For example, each element in the (i, j) th position, in the resulting matrix C, is the sum of the elements of the product whose first matrix in row i has the corresponding elements in column j of the second matrix.

In MATLAB, matrix multiplication uses * operator.

Detailed examples

Create a script file in MATLAB with the following code:

a = [ 1 2 3; 2 3 4; 1 2 5]b = [ 2 1 3 ; 5 0 -2; 2 3 -1]prod = a * b

Run the file and display the following results:

a = 1     2     3        2     3     4    1     2     5b = 2     1     3    5     0    -2    2     3    -1prod = 18    10    -4       27    14    -4       22    16    -6

Determinant of MATLAB matrix

MATLAB's instruction to calculate the value of the corresponding matrix determinant is: d=det(A). This instruction returns the determinant of matrix A and assigns the obtained value to d. If a contains only integer terms, the result D is also an integer.

Detailed examples

Create a script file in MATLAB with the following code:

a = [ 1 2 3; 2 3 4; 1 2 5]det(a)

Run the file and display the following results:

a =     1     2     3     2     3     4     1     2     5ans =    -2

MATLAB inverse matrix

The inverse matrix of matrix A in MATLAB is recorded as − a − 1, and the following relationship holds:

AA−1 = A−1A = 1

Not every matrix in MATLAB has an inverse matrix. For example, if the determinant of a matrix is zero, the inverse of the matrix does not exist. Such a matrix is singular.

In MATLAB, the inv function is used to calculate the inverse matrix: the inverse matrix A is inv(A)

Detailed examples

Create a script file in MATLAB and enter the following code:

a = [ 1 2 3; 2 3 4; 1 2 5]inv(a)

Run the file and display the following results:

a =     1     2     3     2     3     4     1     2     5ans =   -3.5000    2.0000    0.5000    3.0000   -1.0000   -1.0000   -0.5000         0    0.5000

 

Keywords: MATLAB

Added by lives4him06 on Sun, 16 Jan 2022 18:27:23 +0200