0
0
MATLABdata~5 mins

Matrix multiplication (*) in MATLAB

Choose your learning style9 modes available
Introduction
Matrix multiplication lets you combine two matrices to get a new matrix that represents combined information.
When you want to combine data from two tables in a way that matches rows and columns mathematically.
When calculating transformations in graphics, like rotating or scaling shapes.
When solving systems of linear equations using matrices.
When working with neural networks to combine weights and inputs.
When performing operations in physics or engineering that involve vectors and matrices.
Syntax
MATLAB
C = A * B
A and B must have compatible sizes: number of columns in A must equal number of rows in B.
The result C will have the number of rows of A and the number of columns of B.
Examples
Multiply two 2x2 matrices to get a 2x2 matrix.
MATLAB
A = [1 2; 3 4];
B = [5 6; 7 8];
C = A * B;
Multiply a 2x3 matrix by a 3x2 matrix to get a 2x2 matrix.
MATLAB
A = [1 2 3; 4 5 6];
B = [7 8; 9 10; 11 12];
C = A * B;
Sample Program
This program multiplies a 2x3 matrix A by a 3x2 matrix B and displays the result.
MATLAB
A = [1 0 2; -1 3 1];
B = [3 1; 2 1; 1 0];
C = A * B;
disp('Result of A * B is:');
disp(C);
OutputSuccess
Important Notes
If the sizes of A and B do not match for multiplication, MATLAB will give an error.
Use the element-wise multiplication operator (.*) if you want to multiply corresponding elements instead.
Matrix multiplication is not commutative: A * B is usually not the same as B * A.
Summary
Matrix multiplication combines two matrices into one by multiplying rows by columns.
The number of columns in the first matrix must match the number of rows in the second.
Use * for matrix multiplication and .* for element-wise multiplication.