Matlab2011_c3.pdf
(
531 KB
)
Pobierz
Chapter 3: Matrices, Calculations and Logical operations
3.1 Matrices
As mentioned in the earlier chapter, you can have lists of numbers as well as of letters.
These list of numbers can be one-dimensional, in which case they called a
vector
. 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]
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]
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)
ans =
4 3
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'
vect1 =
1 2 4 6 3
vect2 =
1
2
4
6
3
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 of a vector or a matrix.
Remember the command whos which will tell you the size of all your variables at once?
Use
whos
to look at the size of
mat1
,
mat2
,
vect1
and
vect2
.
whos
Name Size Bytes Class Attributes
M1 2x3 48 double
SPM5Path 1x34 68 char
V1 1x4 32 double
V2 1x4 32 double
ans 1x2 16 double
mat 3x5 120 double
mat1 4x3 96 double
mat2 4x3 96 double
vect1 1x5 40 double
vect2 5x1 40 double
3.2 Addition and subtraction
You can perform various calculations on matrices and arrays. You can add a single number
(generally called a
scalar
) to a vector.
vect1+3
ans =
4 5 7 9 6
You can subtract a scalar.
vect2-3
ans =
-2
-1
1
3
0
Scalar?
In physics a scalar is a value that only has magnitude (and not direction). In programming a scalar
is defined as a quantity that can only hold a single value at a time, i.e. a single number or
character. Generally the expression
scalar
tends to be used to refer to numbers rather than
characters.
Matrix dimensions must agree?
You get this error if you try to add vectors of inappropriate sizes. To successfully add two
vectors they must be the same orientation as well as the same length – i.e. you can't add a tall
thin vector to a short fat vector. The same goes for other operations (subtraction, point-wise
multiplication & division and matrix multiplication & division) and applies to matrices as
well as vectors.
When you get this error the first thing you should do is check the size of all the variables that
you are trying to manipulate, and try to work out why Matlab thinks they are mismatched in
size or shape. 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 play with a few examples of your own and
make sure you understand this.
You can add a vector onto itself
vect1+vect1
ans =
2 4 8 12 6
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.
vect3= [1 2 3 4]
vect4=[ 1 3 5 2]';
vect3+vect4;
vect3 =
1 2 3 4
??? Error using ==> plus
Matrix dimensions must agree.
mat1=[1 2 3; 4 5 6];
mat2=[1 2; 3 4; 5 6];
mat1-mat2'
ans =
0 -1 -2
2 1 0
mat1-mat2
??? Error using ==> minus
Matrix dimensions must agree.
3.3 Scalar multiplication and division
Here you simply use the symbols
*
and
/.
You can a multiply a scalar with another scalar,
with a vector, or with a matrix.
2*3
ans =
6
2/3
ans =
0.6667
vect1*3
ans =
3 6 12 18 9
vect1/2
ans =
0.5000 1.0000 2.0000 3.0000 1.5000
mat1*0.5
ans =
0.5000 1.0000 1.5000
2.0000 2.5000 3.0000
3+1*4
ans =
7
3*4+1
ans =
13
Multiplication & division has priority over addition & subtraction. So the statements above
are the equivalent of 3+(1*4) and (3*4)+1. If you want to do addition or subtraction before
multiplication or division you need to use brackets.
(3+1)*4
ans =
16
3*(4+1)
ans =
15
Vector multiplication and
division
As you may or may not
remember from high school
math there are actually two
sorts of multiplication and
two sorts of division when
you are multiplying vectors
with each other (or matrices with each other). They are called
point-wise
and
inner-product
multiplication respectively.
3.4 Point-wise vector multiplication and division
Point-wise
(or
element by element
) multiplication and division is the simplest. In Matlab
these are computed to using .*
and ./ (notice the period character).
V1=[1 2 3 4];
V2=[2 3 4 5];
V1.*V2
ans =
2 6 12 20
V2.*V1
ans =
2 6 12 20
V1./V2
ans =
0.5000 0.6667 0.7500 0.8000
V2./V1
ans =
2.0000 1.5000 1.3333 1.2500
Each element in the first vector is multiplied by the corresponding element in the second
vector. As with addition and subtraction, the vectors must be the same shape. Note what
happens if we make one of the two vectors tall and thin (using the
' transpose
). We get
an error stating that we are trying to multiply two things of different shapes.
Plik z chomika:
miery
Inne pliki z tego folderu:
male.zip
(67 KB)
Matlab Class Chapter 1.pdf
(44 KB)
Matlab Class Chapter 2.pdf
(49 KB)
Matlab Class Chapter 3.pdf
(66 KB)
Matlab Class Chapter 4.pdf
(38 KB)
Inne foldery tego chomika:
MatLab
XML4MAT
Zgłoś jeśli
naruszono regulamin