multiply two matrices in matlab

To multiply two matrices in MATLAB, the dot product function (.) or matrix multiplication function (*) can be used. The dot product is used when multiplying corresponding elements of two matrices, while matrix multiplication is used when multiplying two matrices to produce a third matrix.

Here is an example of using the dot product function to multiply two matrices:

main.m
A = [1 2 3; 4 5 6]; 
B = [7 8; 9 10; 11 12];
C = A .* B; 
58 chars
4 lines

In this example, matrices A and B have dimensions 2x3 and 3x2 respectively. The dot product of these two matrices is obtained by multiplying element-wise the corresponding elements of each matrix. The resulting matrix C is also of the dimension 2x3.

Alternatively, the matrix multiplication function can be used as:

main.m
A = [1 2 3; 4 5 6]; 
B = [7 8; 9 10; 11 12];
C = A * B; 
57 chars
4 lines

In this case, the number of columns in matrix A must be equal to the number of rows in matrix B. The resulting matrix C has dimensions 2x2 as a result of the matrix multiplication.

It is important to also note that the order of the matrices in multiplication matters, i.e. AB is not always equal to BA.

gistlibby LogSnag