Menu
For example, you can add two or more 3 × 3, 1 × 2, or 5 × 4 matrices. You cannot add a 2 × 3 and a 3 × 2 matrix, a 4 × 4 and a 3 × 3, etc. The number of rows and columns of all the matrices being added must exactly match. If the matrices are the same size, matrix addition is performed by adding the corresponding elements in the matrices. Title: Product Matrix R1.18.indd Author: BillY Created Date: 4/30/2018 9:47:01 AM.
This example shows basic techniques and functions for working with matrices in the MATLAB® language.
First, let's create a simple vector with 9 elements called
a
.Now let's add 2 to each element of our vector,
a
, and store the result in a new vector.Notice how MATLAB requires no special handling of vector or matrix math.
Creating graphs in MATLAB is as easy as one command. Let's plot the result of our vector addition with grid lines.
MATLAB can make other graph types as well, with axis labels.
MATLAB can use symbols in plots as well. Here is an example using stars to mark the points. MATLAB offers a variety of other symbols and line types.
Markdown editor online. One area in which MATLAB excels is matrix computation.
Creating a matrix is as easy as making a vector, using semicolons (;) to separate the rows of a matrix.
We can easily find the transpose of the matrix
A
.Now let's multiply these two matrices together.
Note again that MATLAB doesn't require you to deal with matrices as a collection of numbers. MATLAB knows when you are dealing with matrices and adjusts your calculations accordingly.
Instead of doing a matrix multiply, we can multiply the corresponding elements of two matrices or vectors using the .* operator.
Let's use the matrix A to solve the equation, A*x = b. We do this by using the (backslash) operator.
Now we can show that A*x is equal to b.
MATLAB has functions for nearly every type of common matrix calculation.
There are functions to obtain eigenvalues ..
.. as well as the singular values.
The 'poly' function generates a vector containing the coefficients of the characteristic polynomial.
The characteristic polynomial of a matrix
A
isWe can easily find the roots of a polynomial using the
roots
function.These are actually the eigenvalues of the original matrix.
MATLAB has many applications beyond just matrix computation.
To convolve two vectors ..
.. or convolve again and plot the result.
At any time, we can get a listing of the variables we have stored in memory using the
who
or whos
command.You can get the value of a particular variable by typing its name.
You can have more than one statement on a single line by separating each statement with commas or semicolons.
If you don't assign a variable to store the result of an operation, the result is stored in a temporary variable called
ans
.As you can see, MATLAB easily deals with complex numbers in its calculations.
Related Topics
By Murray Bourne, 26 Nov 2015
Reader Nour recently wrote:
Hello! ?
I just had a problem and got stuck when i tried to multiply (A.B).C where A ,B and C are three matrices with dimensions 1x3, 3x1 and 3x1 respectively. I got the product of (A.B) with a 1x1 dimensional matrix.
The question is: can a 1x1 matrix be a scalar? So should I just stop and say that 1x1 and 3x1 can't be multiplied?
Or is the matrix [2] = 2 a scalar (for example)?
And continue multiplying the scalar (AB) with the matrix C?
This was an interesting question. First, let me explain the issue a bit.
Multiplying matrices
We learn in the Multiplying Matrices section that we can multiply matrices with dimensions (m × n) and (n × p) (say), because the inner 2 numbers are the same (both n). The result will be a vector of dimension (m × p) (these are the outside 2 numbers).
Now, in Nour's example, her matrices A, B and C have dimensions 1x3, 3x1 and 3x1 respectively.
So let's invent some numbers to see what's happening.
Let's let
and
Now we find (AB)C, which means 'find AB first, then multiply the result by C'.
Next,
The above line is Nour's dilemma. Are we allowed to multiply the above? We have the following situation:
(1 × 1) matrix × (3 × 1) matrix
According to what I said above, since the inside numbers (1 and 3) are different, then we can't multiply the matrices.
However, if we regard a (1 × 1) matrix as a scalar, we can multiply them just fine, and get the following result:
Which is correct?
Software solutions
Scientific Notebook gave me the following for the first step:
Matrix Multiplication 3*1 1*3
That is, it regards a 1×1 matrix as a scalar. It was fine with the scalar times matrix step and gave the same as my second result above.
However, it choked (it refused to answer) when I tried to do it all in one go, like this:
1 3 Times 1 3
Even giving it a hint (by putting brackets) didn't help:
Wolfram|Alpha regards the result of AB as a (1 × 1) matrix. Here's a screen shot:
It did not behave well and gave rather strange results when trying to do the whole thing. Here's a screen shot of its answer for ABC, where it regarded parts of the question as a vector, and other parts as a matrix:
SageMath (the free cloud-based computer algebra system) also regards the result of AB as a (1 × 1) matrix. It appropriately chokes when trying to do the whole thing. Here's a screen shot:
What do the forums say?
Here's one conversation on Stackexchange: Are one-by-one matrices equivalent to scalars?
This next one has a contradictory conclusion: Is a one by one matrix just a number scalar?
And there's more enlightenment here on this Google+ discussion.
What I told Nour
My feel is that your statement 'So i will just stop and say that 1x1 and 3x1 can't be multiplied' is correct. While the outer numbers are the same, the inner ones are not, so you can't multiply them.
What do you think?
Conclusion
Mathematics is not always a consistently behaved beast, despite what many text books say.
See the 10 Comments below.
Related posts:
- Multiplying matrices This article points to 2 interactives that show how to multiply matrices..
- Matrices and Flash games This is an interesting tutorial on how matrices are used in Flash animations..
- Matrix operations applet Here's some mathematical background to the matrix operations applet here on IntMath. It involves matrix..
- Updated matrix interactive Here's an applet where you can investigate how matrix operations work..
- Inverse of a matrix by Gauss-Jordan elimination Here's a method for finding inverses of matrices which reduces the chances of getting lost..