0% found this document useful (0 votes)
63 views

3 Arrayvsmatrix

This document discusses the differences between array and matrix operations in MATLAB. Array operations perform element-wise computations on vectors and matrices, while matrix operations follow the rules of linear algebra. Key differences include: - Array operations use implicit expansion to allow operations between differently sized arrays, while matrix operations require compatible sizes - Matrix multiplication computes inner products between rows and columns according to the standard formula, while array multiplication performs element-wise multiplication - Several examples demonstrate adding, subtracting, and multiplying arrays and matrices to illustrate their different behaviors.

Uploaded by

Sheila Rove
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

3 Arrayvsmatrix

This document discusses the differences between array and matrix operations in MATLAB. Array operations perform element-wise computations on vectors and matrices, while matrix operations follow the rules of linear algebra. Key differences include: - Array operations use implicit expansion to allow operations between differently sized arrays, while matrix operations require compatible sizes - Matrix multiplication computes inner products between rows and columns according to the standard formula, while array multiplication performs element-wise multiplication - Several examples demonstrate adding, subtracting, and multiplying arrays and matrices to illustrate their different behaviors.

Uploaded by

Sheila Rove
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LABSHEET #3 Array Vs Matrix BIM33203

BIM33203 – IMAGE PROCESSING


SEMESTER 2 SESSION 2022/2023

Labsheet #3
Title : Array Vs Matrix
Objectives : At the end of the lab session, students should be able to:
(i) Apply, process, and analyze array and matrix.
(ii) Manipulate the effects of variation of array and
matrix arithmetic operators.
Date : 20 April 2023 (Thursday)

ARRAY VS. MATRIX OPERATIONS

1. Introduction
MATLAB® has two different types of arithmetic operations: array operations and matrix
operations. You can use these arithmetic operations to perform numeric computations, for
example, adding two numbers, raising the elements of an array to a given power, or multiplying
two matrices.
Matrix operations follow the rules of linear algebra. By contrast, array operations execute element
by element operations and support multidimensional arrays. 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 unnecessary.

2. Array Operations
Array operations execute element by element operations on corresponding elements of vectors,
matrices, and multidimensional arrays. If the operands have the same size, then each element in
the first operand gets matched up with the element in the same location in the second operand. If
the operands have compatible sizes, then each input is implicitly expanded as needed to match the
size of the other.

1
LABSHEET #3 Array Vs Matrix BIM33203

As a simple example, you can add two vectors of the same size.
A = [1 1 1]
A=

1 1 1

B = [1 2 3]
B=

1 2 3

A+B
ans =

2 3 4

If one operand is a scalar and the other is not, then MATLAB implicitly expands the scalar to be
the same size as the other operand. For example, you can compute the element-wise product of a
scalar and a matrix.
A = [1 2 3; 1 2 3]
A=

1 2 3
1 2 3

3.*A
ans =

3 6 9
3 6 9

The implicit expansion also works if you subtract a 1-by-3 vector from a 3-by-3 matrix because
the two sizes are compatible. When you perform the subtraction, the vector is implicitly expanded
to become a 3-by-3 matrix.
A = [1 1 1; 2 2 2; 3 3 3]
A=

1 1 1
2 2 2
3 3 3

2
LABSHEET #3 Array Vs Matrix BIM33203

m = [2 4 6]
m=

2 4 6

A - m
ans =

-1 -3 -5
0 -2 -4
1 -1 -3

A row vector and a column vector have compatible sizes. If you add a 1-by-3 vector to a 2-by-1
vector, then each vector implicitly expands into a 2-by-3 matrix before MATLAB executes the
element-wise addition.
x = [1 2 3]
x=

1 2 3

y = [10; 15]
y=

10
15

x + y
ans =

11 12 13
16 17 18

If the sizes of the two operands are incompatible, then you get an error.
A = [8 1 6; 3 5 7; 4 9 2]
A=

8 1 6
3 5 7
4 9 2

m = [2 4]
m=

2 4

3
LABSHEET #3 Array Vs Matrix BIM33203

A - m
Matrix dimensions must agree.

The following table provides a summary of arithmetic array operators in MATLAB.

Operator Purpose Description


+ Addition A+B adds A and B.

+ Unary plus +A returns A.

- Subtraction A-B subtracts B from A

- Unary minus -A negates the elements of A.

.* Element-wise multiplication A.*B is the element-by-element product of A and B.

.^ Element-wise power A.^B is the matrix with elements A(i,j) to the


B(i,j) power.
./ Right array division A./B is the matrix with elements A(i,j)/B(i,j).

.\ Left array division A.\B is the matrix with elements B(i,j)/A(i,j).

.' Array transpose A.' is the array transpose of A. For complex matrices,
this does not involve conjugation.

3. Matrix Operations
Matrix operations follow the rules of linear algebra and are not compatible with multidimensional
arrays. The required size and shape of the inputs in relation to one another depending on the
operation. For non-scalar inputs, the matrix operators generally calculate different answers than
their array operator counterparts.
For example, if you use the matrix right division operator, /, to divide two matrices, the matrices
must have the same number of columns. But if you use the matrix multiplication operator, *, to
multiply two matrices, then the matrices must have a common inner dimension. That is, the
number of columns in the first input must be equal to the number of rows in the second input. The
matrix multiplication operator calculates the product of two matrices with the formula,

4
LABSHEET #3 Array Vs Matrix BIM33203

To see this, you can calculate the product of two matrices.


A = [1 3;2 4]
A=

1 3
2 4

B = [3 0;1 5]
B=

3 0
1 5

A*B
ans =

6 15
10 20

The previous matrix product is not equal to the following element-wise product.
A.*B
ans =

3 0
2 20

The following table provides a summary of matrix arithmetic operators in MATLAB.

Operator Purpose Description


* Matrix multiplication C = A*B is the linear algebraic product of the matrices
A and B. The number of columns of A must equal the
number of rows of B.
\ Matrix left division x = A\B is the solution to the equation Ax = B.
Matrices A and B must have the same number of rows.
/ Matrix right division x = B/A is the solution to the equation xA = B. Matrices
A and B must have the same number of columns. In
terms of the left division operator, B/A = (A'\B')'.
^ Matrix power A^B is A to the power B, if B is a scalar. For other values
of B, the calculation involves eigenvalues and eigenvectors.
' Complex conjugate transpose A' is the linear algebraic transpose of A. For complex
matrices, this is the complex conjugate transpose.

5
LABSHEET #3 Array Vs Matrix BIM33203

4. Extra Exercises
Array Creation
To create an array with four elements in a single row, separate the elements with either a comma
(,) or a space.
a = [1 2 3 4]
a = 1×4

1 2 3 4

This type of array is a row vector.


To create a matrix that has multiple rows, separate the rows with semicolons.
a = [1 2 3; 4 5 6; 7 8 10]
a = 3×3

1 2 3
4 5 6
7 8 10

Another way to create a matrix is to use a function, such as ones, zeros, or rand. For example,
create a 5-by-1 column vector of zeros.
z = zeros(5,1)
z = 5×1

0
0
0
0
0

Matrix and Array Operations


MATLAB allows you to process all of the values in a matrix using a single arithmetic operator or
function.
a + 10
ans = 3×3

11 12 13
14 15 16
17 18 20

6
LABSHEET #3 Array Vs Matrix BIM33203

sin(a)
ans = 3×3

0.8415 0.9093 0.1411


-0.7568 -0.9589 -0.2794
0.6570 0.9894 -0.5440

To transpose a matrix, use a single quote ('):


a'
ans = 3×3

1 4 7
2 5 8
3 6 10

You can perform standard matrix multiplication, which computes the inner products between rows
and columns, using the * operator. For example, confirm that a matrix times its inverse returns the
identity matrix:
p = a*inv(a)
p = 3×3

1.0000 0 0
0.0000 1.0000 0
0.0000 -0.0000 1.0000

Notice that p is not a matrix of integer values. MATLAB stores numbers as floating-point values,
and arithmetic operations are sensitive to small differences between the actual value and its
floating-point representation. You can display more decimal digits using the format command:
format long
p = a*inv(a)
p = 3×3

1.000000000000000 0 0
0.000000000000002 1.000000000000000 0
0.000000000000002 -0.000000000000004 1.000000000000000

Reset the display to the shorter format using


format short
format affects only the display of numbers, not the way MATLAB computes or saves them.

7
LABSHEET #3 Array Vs Matrix BIM33203

To perform element-wise multiplication rather than matrix multiplication, use the .* operator:
p = a.*a
p = 3×3

1 4 9
16 25 36
49 64 100

The matrix operators for multiplication, division, and power each have a corresponding array
operator that operates element-wise. For example, raise each element of a to the third power:
a.^3
ans = 3×3

1 8 27
64 125 216
343 512 1000

Concatenation
Concatenation is the process of joining arrays to make larger ones. In fact, you made your first
array by concatenating its individual elements. The pair of square brackets [ ] is the concatenation
operator.
A = [a,a]
A = 3×6

1 2 3 1 2 3
4 5 6 4 5 6
7 8 10 7 8 10

Concatenating arrays next to one another using commas is called horizontal concatenation. Each
array must have the same number of rows. Similarly, when the arrays have the same number of
columns, you can concatenate vertically using semicolons.
A = [a; a]
A = 6×3

1 2 3
4 5 6
7 8 10
1 2 3
4 5 6
7 8 10

8
LABSHEET #3 Array Vs Matrix BIM33203

Complex Numbers
Complex numbers have both real and imaginary parts, where the imaginary unit is the square root
of -1.
sqrt(-1)

ans = 0.0000 + 1.0000i

To represent the imaginary part of complex numbers, use either i or j.


c = [3+4i, 4+3j; -i, 10j]
c = 2×2 complex

3.0000 + 4.0000i 4.0000 + 3.0000i


0.0000 - 1.0000i 0.0000 +10.0000i

You might also like