Array Operations: Deleting Row or Column
Array Operations: Deleting Row or Column
Array Operations: Deleting Row or Column
ans =
9
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.
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+b
ans =
3 3 3 3
>> x+y
ans =
2 3 4
5 6 7
>> t+v
7
2- Subtraction –
Vector (V) Matrix (M)
Size V1=size V2 size M1=size M2
V1-V2 ≠ V2-V1 M1-M2 ≠ M2-M1
>> a-b
ans =
1 1 0 1
>> b-a
ans =
-1 -1 0 -1
A=
1 2 3
4 5 6
7 8 9
B=
10 20 30
40 50 60
70 80 90
>> A-B
ans =
-9 -18 -27
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 mn . 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=
2 18 32
50 98 128
9
a =[ 1:3 ]
a=
1 2 3
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 =
x =
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
x =
6- Power (.^)
Fixed-point array power (.^)
Syntax
c = a.^k
>> a.^2
ans =
4 4 4
11