ECE343: Computer Aided Design (CAD)
Lecture 4
Arrays in Matlab
Functions in Matlab
Dr. Sawsan Abdellatif 1
2
3
Syntax
This function creates a large matrix B consisting of an MxN copies
of A.
This function has the following three arguments:
4
5
6
This function changes the dimensions of the array X to the new
size of MxN.
Syntax
The elements are taken from the source array X in a column-by-
column fashion.
7
8
Number of rows and columns of an array can be calculated using “size”
function.
Syntax >> [m,n]=size(X)
m is number of rows, n is number of columns
To find the total number of elements in the array X:
9
You can convert an array to a column vector using the colon (:) operator.
For example, to convert the array X to a column vector, type:
Note that the elements have been extracted from the array X, in a column-by-
column fashion.
10
Arrays can be concatenated (combined) together to produce larger arrays.
Note that here we have used the semicolon (;) to combine Z and X arrays
in the vertical direction.
11
Note that here we have used the comma (,) to combine X and R arrays in
the horizontal direction.
12
Two methods to access the elements within an array:
Row-and-column indexing.
Linear indexing.
An element in the array X is referred to as Xm,n , where m
refers to the row number and n refers to the column number.
To access the last element in the first row of X,
To access the last element in the third column of X,
13
>>a=X(1)
a=
3
>>b=X(7)
b=
8
>>c=X(12) >>c=X(end)
c= c=
10 10
14
You can use the colon operator (:) to access a Row in an array.
To access the first row:
To access the last row,
To access the last two rows
To access the first and the third rows,
15
You can use the colon operator (:) to access a Column in an array.
To access the first column:
To access the last column,
To access the first and second columns,
16
To access the first and second rows of the third column.
To access the second, third, and fourth columns of the second row
To access the elements that are encircled by the rectangular area below:
17
To find the indices of the elements whose values is
greater than 7,
a= b=
This output means that the elements E(3,2) and E(1,3) are greater than 7.
To find the indices of the elements in the array E whose value is less than
3,
c= d=
To find the values of the elements in E that have values that are less than
3,
18
To find the elements in X whose values are greater than
3.
To access the elements of Y which have the corresponding positions
within the array to locations in the array X where X is greater than 3.
19
commands description
mean2 mean of matrix elements
std2 Standard deviation of matrix elements
det Matrix Determinant
inv Matrix inverse.
diag Matrix diagonal
trace Sum of diagonal elements
max(max(A)) Maximum element in matrix A
min(min(A)) Minimum element in matrix A
zeros Create zeros array.
ones Create ones array.
eye Create Identity matrix.
rand Create array with random numbers between 0 and 1 20
Let us plot the function Z = 𝑋 2 − 𝑌 2
where X is in the range of [-2, 2] and Y is in the range of [-3, 3].
21
To improve the resolution of the 3D
plot, increase the number of points
within the X and Y arrays
22
The surf function plots an array as a surface
The relationship between the color in the
surface plot and the value of Z can be
shown using colorbar command
23
% y is 3x3x3 array
% a is 2x2x3 array
24
25
A Matlab function is a collection of commands that does a specific task
and must have a unique name.
Functions can improve Code Readability and Reusability.
Example:
Example:
Function name: “Cartesian2polar”
Inputs: x,y
Outputs: r, theta
26
You can call a Matlab function from:
A script file,
The Command Window,
Another function
27
The Matlab Editor pops up and write your function as :
Delete everything in the Editor and type the following code in the Editor
Save this function as add2.m
The name of the file MUST be
exactly the same as the name of
the function and must be
followed by the .m extension.
28
You must give the correct number of input arguments or you will get an error.
Matlab executes the add2 function, and the returned result is assigned to the
variable a.
Note that the function arguments x, y and
z do not actually appear in the
Workspace window and they do not exist
in Matlab memory, as they were only
temporary function variables.
29
30
To call this function:
31
Many functions do not calculate values, but rather accomplish a task such as
printing formatted output.
Example
Call
since the function isn’t returning any value, it cannot be called from an
assignment statement:
32
A variable that is created within a function has a limited scope. This means
that this variable can be only accessed or modified by this function.
This variable is called a local variable.
Answer:
The variables a, r, and c are local
Call this function variables to the function pow. To
check this for the variable r, type at
the Command Prompt
33
A variable created in the Command Window cannot be accessed by a
function.
Example:
Even though we have created the variable r in the Command Window,
the pow function cannot access this variable.
Similarly, a variable created in a script file cannot be accessed by a
function. 34
Write a MATLAB function to calculate the 2D distance between
two points. Then call this function from the command window
using any two points and show the expected result.
35
36