Matlab Class Chapter 2.pdf

(49 KB) Pobierz
Chapter 2 - 1
Chapter 2
Matrices and Calculations
As mentioned in the earlier chapter, you can have lists of numbers as well as of letters.
These list of numbers can be either one-dimensional, in which case they are called a
vector, or an array. When they are two, three, or more-dimensional they are called a
matrix .
>>mat1=[1 54 3; 2 1 5; 7 9 0; 0 1 0]
>>mat2=[1 54 3
2 1 5
7 9 0
0 1 0]
Take a look at mat1 and mat2. As you can see there is more than one way of entering a matrix. mat1 and
mat2 were entered differently, but both have 4 rows and 3 columns. When entering matrices a semi-colon
is the equivalent of a new line. You can find the size of matrices using the command “size.
>>size(mat1)
For a two dimensional matrix the first value in size is the number of rows. The second value of size is
the number of columns.
Now try:
>>vect1=[1 2 4 6 3]
>>vect2=vect1’
Vectors can be tall instead of long, (the little symbol below the double quote on your keyboard) is a
transpose , and allows you to swop rows and columns.
Remember there is also the command whos which will tell you the size of all your variables.
Use whos to look at the size of mat1, mat2, vect1 and vect2.
>> whos
What does mat1’ look like?
You can perform various calculations on matrices and arrays.
You can add a single number (also called a scalar) to a vector
>>vect1+3
You can subtract
>> vect2-3
You can add a vector onto itself
>>vect1+vect1
You can also add two vectors as long as they are the same size. You can’t add vect1 and vect2 together
since they are different sizes.
>>vect1+vect2
??? Error using ==> plus
Matrix dimensions must agree.
The reason you got an error is that you can’t multiply something with 1 row and 4 columns, with something
else that has 4 rows and 1 columns.
>>vect3=[2 4 8 12 6]
>>vect1+vect3
These two vectors are the same size, so you can add them together, So far it’s easy.
Chapter 2 - 2
Point-wise multiplication and division
But when you want to add two vectors or matrices to each other you need to know that there are two sorts of
multiplication and two sorts of division. The simple kind of multiplication and division is called array
multiplication. You do this using the symbols .* and ./
>>vect1.*3
>>mat1.*0.5
>>vect1./2
You can also multiply or divide one vector by another, element by element. Each element in the first vector
is multiplied by the corresponding element in the second vector. Note that the vectors must be the same
shape
>>vect1.*vect3
>>vect1./vect3
>>vect3./vect1
Watch out for the transpose in the next 2 examples:
>>vect1.*vect2’
>>vect1.*vect3’
??? Error using ==> plus
Matrix dimensions must agree.
You will get this error a lot. What an error like this means is that the two vectors or matrices that you are
trying to perform an operation on (such as multiplication) aren’t the right size or shape to do what you are
doing. Lots of operations are fussy about making sure the sizes of the vectors or matrices are consistent. So
when you get this error the first thing you should do is check the size of all the variables that you are tying
to manipulate, and try to work out whether they might be different sizes. Often it’s the case that simply
transposing one of the variables is all you need to do. This is an important thing to understand, so don’t rush
through the examples above. Make sure you understand what’s going on.
>> mat1./vect1
??? Error using ==> rdivide
Matrix dimensions must agree.
This is not going to happen for you, regardless of how you transpose mat1 and vect1. vect1 and mat1 will
never be the same size, no matter what you do.
So, with point-wise multiplication and division (which is what you will be doing most of the time) you can:
1) multiply or divide a matrix or vector by a single number (either * or .* is correct to use)
2) multiply or divide a matrix or vector by a matrix/vector that is the same size (make sure you use .* ).
It’s best to get into the habit of using .* all the time, unless you are specifically using matrix multiplication
(which you will only use rarely)
The second kind of multiplication and division is matrix multiplication and matrix division. There are two
types of matrix multiplication.
Matrix Multiplication
Outer product
Z=X*Y – the first vector is tall & thin, the second vector is short & fat .
Actually matrix multiplication isn’t used very often in experimental programming, though vision scientists
often find it useful for making filters for image processing and so on.
>B = [1; 2; 3; 4; 5]
B =
1
2
3
4
5
Chapter 2 - 3
>C = [2 3 4 3 2]
C =
2 3 4 3 2
> whos
Name Size Bytes Class
B 5x1 40 double array
C 1x5 40 double array
The first vector is tall and thin, and the second vector is short and fat. This means we are calculating the
outer product of the two vectors:
>B*C
ans =
2 3 4 3 2
4 6 8 6 4
6 9 12 9 6
8 12 16 12 8
10 15 20 15 10
BC
BC
*C
*C
1
1
2
2
3
3
4
4
3
3
2
2
* [
* [
]
]
=
=
2
2
3
3
4
4
3
3
2
2
2
2
4
4
6
6
8
8
6
6
4
4
6
6
9
9
12
12
9
9
6
6
3
3
8
8
12
12
16
16
12
12
8
8
4
4
10
10
15
15
20
20
15
15
10
10
5
5
If we transpose both B and C and reverse the multiplication order, then the number of rows in C’ again
matches the number of columns in B’, and we can calculate another outer product, which is in fact the
transpose of the previous outer product:
> C'*B'
ans =
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
3 6 9 12 15
2 4 6 8 10
904006318.001.png 904006318.002.png
Chapter 2 - 4
C´*B´
C´*B´
2
2
2
2
4
4
6
6
8
8
10
10
* [
* [
5
5
=
=
1
1
2
2
3
3
4
4
3
3
3
3
6
6
9
9
12
12
15
15
4
4
8
8
12
12
16
16
20
20
4
4
3
3
6
6
9
9
12
12
15
15
3
3
2
2
4
4
6
6
8
8
10
10
2
2
Inner product
Here the first vector is short & fat, and the second vector is tall & thin.
>C*B
CB
CB
C*B
C*B
1
1
[
[
]
]
=
=
=42
=42
2
2
3
3
4
4
3
3
2
2
*
*
2
2
(1*2)
+ (3*2)
+(4*3)
+(3*4)
+(2*5)
(1*2)
+ (3*2)
+(4*3)
+(3*4)
+(2*5)
3
3
4
4
5
5
With inner products you can again transpose both vectors and swop the order of the multiplication. This time
you get the same answer – 42.
C´*B´
C´*B´
2
2
[
[
]
]
=
=
=42
=42
*
*
1
1
2
2
3
3
4
4
5
5
(1*2)
+ (2*3)
+(3*4)
+(4*3)
+(5*2)
(1*2)
+ (2*3)
+(3*4)
+(4*3)
+(5*2)
3
3
4
4
3
3
2
2
3d-4d matrices & more matrix manipulations
Matrices can be 3, 4 or more dimensions, though some commands won’t work for matrices with more than 2
or 3 dimensions
904006318.003.png 904006318.004.png
Chapter 2 - 5
>mat(1, :,:) =[2 4 3 ; 5 6 7];
>mat(2, :, :)=[1 3 2; 6 1 2];
>size(mat)
>mat
Sometimes it’s useful to convert back and forth between vectors and matrices.
>mat=[1 2 3; 4 5 6]
mat =
1 2 3
4 5 6
>vect=mat(:)
vect =
1
4
2
5
3
6
Strings the matrix mat out a as a vector. Note that the vector first lists all the rows in the first column, then
lists all the rows in the second column, and so on …
>ind=sub2ind(size(mat), 2, 2)
calculates the index in vect corresponding to the 2 nd row and 2 rd column of mat. So:
>vect(ind)
>mat(2,2)
Should give you the same answer. Check out ind2sub and work out what it does.
Program 2 - Calculations.m
Create a new m-file and save it in the MatlabClass/Misc folder as “Calculations.m”.
You’ll notice in the code below that some of the lines are rather long. When you are writing code and a line
is longer than your page you can break it using three dots ... Without those three dots Matlab will treat each
part of the line as a separate command and give you an error message.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
% Calculations.m
% % Example program that carries out a series of
% calculations on two numbers
% % written by IF and GMB 4/2005
clear;
n1=input( 'choose the first number ... ' );
n2=input( 'choose the second number ... ' );
% spit responses out onto the command line
disp([ 'first number is ' , num2str(n1)])
disp([ 'second number is ' , num2str(n2)])
disp([num2str(n1), '+' , num2str(n2), ...
'=' , num2str(n1+n2)]);
disp([num2str(n1), '-' , num2str(n2), ...
Zgłoś jeśli naruszono regulamin