®

MATLAB® Programming

Matrix Multiplication Example


Click below to go directly to a specific section:
Description | Source Code | Sample Run | Program Notes


Description

The following example will show how Matlab® performs matrix multiplication.  In most languages, matrix multiplication would be performed with the following algorithm or one similar:

matrA (i, j)
matrB (j, k)
matrR (i, k)

for x = 1 to i
    for z = 1 to k
        for y = 1 to j
            matrR(x, z) = matrR(x, z) + matrA(x, y) * matrB(y, z);

The next section will show how nicely it is to implement this in MATLAB


Source Code

This is a basic Matlab® script that when run, will multiply two matrices.

% Name:     mamu.m
% Purpose:  Execute a matrix multiplication
%
% Create the first matrix
matrA = [1 2 3; 4 5 6];
% Create the second matrix
matrB = [1 2 3 4; 5 6 7 8; 9 10 11 12];
% Assign matrix multiplication result to a variable
matrR = matrA * matrB;

Sample Run

>> mamu
>> matrR

matrR =

    38    44    50    56
    83    98   113   128

Program Notes

This program has been tested, it works perfectly without any errors or warnings.


[Back] [Home]


Last modified:  12:55 AM on 12/10/1997