Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

>> A(end:-1:1,end)

ans =
9

>> A([1 3],[2 3])


ans =
2 3
8 9

Deleting row or column


To delete a row or column of a matrix, use the empty vector operator, [ ].
>> A(3,:) = [ ]
A=
1 2 3
4 5 6
Third row of matrix A is now deleted. To restore the third row, we use a technique for
creating a matrix
>> A = [A(1,:);A(2,:);[7 8 0]]
A=
1 2 3
4 5 6
7 8 0
Matrix A is now restored to its original form

Array operations
MATLAB has two different types of arithmetic operations: matrix arithmetic operations
and array arithmetic operations. We have seen matrix arithmetic operations in the previous
lab. Now, we are interested in array operations.

Matrix arithmetic operations


As we mentioned earlier, MATLAB allows arithmetic operations: +, −, ∗, and ˆ to be
carried out on matrices. Thus,
A+B or B+A is valid if A and B are of the same size
A*B is valid if A’s number of column equals B’s number of rows
A^2 is valid if A is square and equals A*A
α*A or A*α multiplies each element of A by α

Array arithmetic operations


On the other hand, array arithmetic operations or array operations for short, are done
element-by-element. The period character, . , distinguishes the array operations from the
matrix operations. However, since the matrix and array operations are the same for addition
(+) and subtraction (−), the character pairs (.+) and (.−) are not used.

5
. If A and B are two matrices of the same size with
elements A = [aij] and B = [bij], then the command

.* Element-by-element multiplication
./ Element-by-element division
.^ Element-by-element exponentiation

>> C = A .* B
produces another matrix C of the same size with elements cij = aijbij. For example, using
the same 3 × 3 matrices,
A= 1 2 3

4 5 6

7 8 9

B = 10 20 30

40 50 60

70 80 90

we have,
>> C = A*B
C=
10 40 90
160 250 360
490 640 810
To raise a scalar to a power, we use for example the command 10^2. If we want the
operation to be applied to each element of a matrix, we use .^2. For example, if we want
to produce a new matrix whose elements are the square of the elements of the matrix A, we
enter
>> A.^2
ans =
1 4 9
16 25 36
49 64 81
The relations below summarize the above operations. To simplify, let’s consider two
vectors U and V with elements U = [ui] and V = [vj].
U. ∗ V produces [u1v1 u2v2 . . . unvn]
U./V produces [u1/v1 u2/v2 . . . un/vn]
U.ˆV produces [uv11 uv22 . . . uvnn]

6
Operation Matrix Array
Addition + +
Subtraction − −
Multiplication ∗ .∗
Division / ./
division \ .\
Exponentiation ˆ .ˆ
Summary of matrix and array operations

1- Addition + +
Vector (V) Matrix (M)
Size V1=size V2 size M1=size M2
V1+V2=V2+V1 M1+M2=M2+M1

>> a=[1 1 1 1];

>> b=[2 2 2 2];

>> a+b

ans =

3 3 3 3

>> x=[1 2 3;4 5 6];

>> y=[1 1 1;1 1 1];

>> x+y

ans =

2 3 4

5 6 7

>> t=[1 2 3 4 5;6 7 8 9 10];

>> v=[3 4 6;6 3 2; 7 8 9];

>> t+v

??? Error using ==> plus

Matrix dimensions must agree.

7
2- Subtraction –
Vector (V) Matrix (M)
Size V1=size V2 size M1=size M2
V1-V2 ≠ V2-V1 M1-M2 ≠ M2-M1

>> a=[2 4 6 8];

>> b=[1 3 6 7];

>> a-b

ans =

1 1 0 1

>> b-a

ans =

-1 -1 0 -1

>> A=[1 2 3;4 5 6;7 8 9]

A=

1 2 3

4 5 6

7 8 9

>> B=[10 20 30;40 50 60;70 80 90]

B=

10 20 30

40 50 60

70 80 90

>> A-B

ans =

-9 -18 -27

-36 -45 -54

-63 -72 -81

8
>> B-A

ans =

9 18 27

36 45 54

63 72 81

3- Multiplication
Multiplication
Matrices are multiplied together in two different ways: element-by-element
multiplication, indicated by the use of a dot (. ) along with the operator, and matrix
multiplication, where the inner dimensions of the matrices must be the same; i.e.,
a- Element-by-element multiplication: A mn . B m n Cm n
b- Matrix multiplication: A m n  B n p  C m p
A=
1 3 4
5 7 8

B=
2 6 8
10 14 16

C = A .* B (23 . * 23 = 23)

C=
2 18 32
50 98 128

A*B (Error: 23 * 23 ≠ 23)


??? Error using ==> *
Inner matrix dimensions must agree.
C = A * B' (23 * 32 = 22)
C=
52 116
116 276
C = A' * B (32 * 23 = 33)
C=
52 76 88
76 116 136
88 136 160

9
a =[ 1:3 ]
a=
1 2 3

a * a' (13 * 31 = 11)


ans =
14

A * a' (23 * 31 = 21)


ans =
19

4- Right array division (.\)

x = B.\A
example
Description
x = B.\A divides each element of A by the corresponding element of B.
 If A and B are arrays, then they must be the same size.
 If either A or B is a scalar, then MATLAB® expands the scalar value into an
appropriately sized array.
Examples
Divide Two Numeric Arrays
A = ones(2, 3);
B = [1 2 3; 4 5 6];
x = B.\A

x =

1.0000 0.5000 0.3333


0.2500 0.2000 0.1667

Divide a Scalar by a Numeric Array


C = 2;
D = [1 2 3; 4 5 6];
x = D.\C

x =

2.0000 1.0000 0.6667


0.5000 0.4000 0.3333

10
5- Left array division (./)

x = B./A
example
Description
x = B./A divides each element of B by the corresponding element of A.
 If A and B are arrays, then they must be the same size.
 If either A or B is a scalar, then MATLAB® expands the scalar value into an
appropriately sized array.
Examples
Divide Two Numeric Arrays
A = ones(2, 3);
B = [1 2 3; 4 5 6];
x = B./A

x =

1 2 3
4 5 6

Divide a Scalar by a Numeric Array


C = 2;
D = [1 2 3; 4 5 6];
x = D./C

x =

0.5000 1.0000 1.5000


2.0000 2.5000 3.0000

6- Power (.^)
Fixed-point array power (.^)

Syntax

c = a.^k

>> a=[2 2 2];

>> a.^2

ans =

4 4 4

11

You might also like