In MATLAB, we can perform different matrix operations using built-in functions or operators.

In MATLAB, we can create matrix-like arrays. A semi-colon separates each row of the matrix (;).

A = [1 2; 3 4];disp(A);

In the code above, we create a $2 \times 2$ matrix and store it in a variable, `A`

. The `disp(A)`

function prints the matrix `A`

.

Let’s go through different matrix operations and how to perform them in MATLAB.

We can perform matrix addition using the `+`

sign, just like in simple math.

A = [1 2; 3 4];B = [5 6; 7 8];C = A + B;disp(C);

In the code above, we create two matrices, `A`

and `B`

. We add them using the `+`

operator and store them in `C`

.

We can perform matrix subtraction using the `-`

sign, just like in simple math.

A = [1 2; 3 4];B = [5 6; 7 8];C = A - B;disp(C);

In the code above, we create two matrices, `A`

and `B`

. We subtract them using the `-`

operator and store them in `C`

.

We can perform matrix multiplication using the `.*`

and `*`

signs for element-wise multiplication and dot product.

A = [1 2; 3 4];B = [5 6; 7 8];% Element wise multiplicationC = A .* B;% Dot productD = A * B;disp(C);disp("-------------")disp(D);

In the code above, we create two matrices, `A`

and `B`

. We multiply them using the `.*`

operator for element-wise multiplication and store them in `C`

. We multiply them using the `*`

operator for the dot product and store them in `D`

.

We can perform matrix transpose using the `'`

sign.

A = [1 2; 3 4];B = A';disp(B);

In the code above, we create a matrix, A. We store its transpose using `'`

and store it in `B`

.

The `inv()`

function is a built-in function for calculating the inverse of a matrix.

A = [1 2; 3 4];B = inv(A);disp(B);

In the code above, `inv(A)`

calculates the inverse of the matrix `A`

.

We can calculate the determinant of a matrix using the built-in function, `det(A)`

.

A = [1 2; 3 4];det_A = det(A);disp(det_A);

In the code above, `det(A)`

calculates the determinant of the matrix, `A.`

We can solve the linear equation system using the `\`

operator. It solves the matrices for values of variables.

A = [1 2; 3 4];b = [5; 6];x = A \ b;disp(x);

`A\b`

solves the linear equations, and we can store it in a solvable variable.

These are just some of the basic operations we can perform on matrices in MATLAB. It offers many other functionalities for matrix operations, including eigenvalue decomposition, singular value decomposition, and more, which we can explore in the MATLAB documentation.

Copyright ©2024 Educative, Inc. All rights reserved

TRENDING TOPICS