0% found this document useful (0 votes)
21 views41 pages

L3 Arrays

Uploaded by

turkisaad65
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)
21 views41 pages

L3 Arrays

Uploaded by

turkisaad65
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

Arrays and Matrices in

MATLAB

Lecture notes, based on “Introduction to MATLAB for


Engineers”, William Palm, 3rd edition, and other
textbooks & resources

EE 201
9/24/2024 C3 OE 1
What is Matrices

?
9/24/2024 C3 OE 2
9/24/2024 C3 OE 3
Creating Numeric Matrix
 We can create numeric array using:

a- Square bracket

b- Colon operator

9/24/2024 C3 OE 4
Square bracket([ ])
 Row Vector:The elements of the row
must be separated by commas or
spaces.
 Example:

9/24/2024 5
C3 OE
 Column Vector:
The elements of the Column
must be separated by:
-semicolon or use the
-transpose notation(‘)
which converts a row vector
into a column vector or vice versa.
For example:
9/24/2024 C3 OE 6
Colon Operator(:)
 The colon operator generates a sequence of numbers that you
can use in creating or indexing into Matrices.

 Numeric Sequence Range

Generate a sequential series of regularly spaced numbers from


first to last using the syntax first:last. For an incremental
sequence from 6 to 17, use:

N = 6:17

9/24/2024 C3 OE 7
Example:

9/24/2024 C3 OE 8
Colon Operator(:)
 Numeric Sequence Step
Generate a sequential series of numbers, each
number separated by a step value, using the
syntax :
first:step:last.
For a sequence from 2 through 38, stepping by 4
between each entry, use:
A = [Link]

9/24/2024 C3 OE 9
Example:

9/24/2024 C3 OE 10
linspace command

 The linspace command also creates a linearly spaced row vector, but
instead you specify the number of values rather than the increment.

The syntax is linspace (x1,x2,n), where x1 and x2 are the lower and upper
limits and n is the number of points.

𝑥2−𝑥1 𝑥2−𝑥1
𝑛= + 1 , where q is step (𝑞 = )
𝑞 𝑛−1

-For example, linspace (5,8,31) is equivalent to [ ? ].


8−5 3
(𝑞 = 31−1 = 30 = 0.1 ) ans=[5.0000 5.1000 5.2000 …………. 7.8000 7.9000 8.0000]

-If n is omitted, generates a row vector of 100 linearly equally spaced points
between x1 and x2.
9/24/2024 C3 OE 11
logspace command

 The logspace command creates an array of


logarithmically spaced elements.
 Its syntax is logspace(a,b,n), where n is the number of points
between 10a and 10b.
For example, x=logspace(-1,1,4) is equivalent to (n=4 --> q= 2/3)
x=10.^[-1:2/3:1] =10.^[-1,-0.333,0.333,1]; produces the vector :
x=[0.1000 , 0.4642 , 2.1544 , 10.000].
If n is omitted, the number of points defaults to 50.
9/24/2024 C3 OE 12
Vector Index
• Vector index points to a particular element
in the array.
• It uses to know the value of element in
the vector .
Example:
Use MATLAB to compute w=5sinu for
u=0,0.1,0.2…..10 and determine the value
of the seventh element in the vector u and
w.
9/24/2024 C3 OE 13
Solution of example

u=linspace(0,10,n)

n=((10-0)/0.1)+1=101

>> u=linspace(0,10,101)

9/24/2024 C3 OE 14
Matrices
 A matrix has multiple rows and columns. For
example, the matrix

M=

has four rows and three columns M(4,3).


 Vectors are special cases of matrices having
one row or one column.

9/24/2024 C3 OE 15
Creating Matrices
 If the matrix is small you can type it row by row, separating the
elements in a given row with spaces or commas and separating the rows
with semicolons. For example, typing:

>>A=[2,4,10;16,3,7]; or A=[2 4 10 ; 16 3 7];

 creates the following matrix:

A=

 Remember, spaces or commas separate elements in different columns,


whereas semicolons separate elements in different rows.

9/24/2024 C3 OE 16
Creating Matrices from Vectors
 Suppose a= [1,3,5] and b=[7,9,11] (row vectors).
Note the difference between the results given by:
[a b] and [a ; b] in the following session:
>>c=[a b] >>d=[a;b]
Concatenating a and b

1 3 5
c = 1 3 5 7 9 11 d=
7 9 11
9/24/2024 C3 OE 17
9/24/2024 C3 OE 18
Array Addressing
 v(:) represents all the row or column elements of the vector v.
 v(2:5) represents the second through fifth elements; that is v(2), v(3), v(4), v(5)

 A(:,3) denotes all the elements in the third column of the matrix A
 A(2,:) denotes all the elements in the second raw of the matrix A

 A(:,2:5) denotes all the elements in the second through fifth columns of A.

 A(2:3,1:3) denotes all the elements in the second and third rows that are also in
the first through third columns.

9/24/2024 C3 OE 19
 You can use array indices to extract a smaller array from
another array. For example, if you first create the array
B.

B=

 then type C=B(2:3,1:3),you can produce the following


array:

c=

9/24/2024 C3 OE 20
9/24/2024 C3 OE 21
Empty array
 The empty(null) array contains no elements and is expressed as [].

 Rows and columns can be deleted by setting the selected row or column
equal to the null array, for example:

-A(3,:)=[ ] deletes the third row in A

-A([1 4],:)=[ ] deletes the first row and fourth rows of A

------------------------------------------------------------------------------------
Let A=

-A(1,5)=3 changes matrix to: A=

9/24/2024 C3 OE 22
6 9 4 0 3
Suppose A =
1 5 7 0 0

B=A(:,5:-1:1) B=
Neg. increment

suppose C=[-4 12 3 5 8] , B(2,:)=C

B= Row replacement

9/24/2024 C3 OE 23
suppose D=[3,8,5;2,-6,9] , D=

E=D([2,2,2],:) E=

Repeats row 2 of D
three times
5 5
S=D(:,[3,3]) 𝑆=
9 9

Repeats column 3
of D tow times

9/24/2024 C3 OE 24
Array Functions

I=find(V) returns the linear indices corresponding


to the nonzero entries of the array V.

Example:

9/24/2024 C3 OE 25
Array Functions

Computes either the number of elements of A if


length(A)
A is a vector or the largest value of m or n if A is
an m × n matrix.

Example

9/24/2024 C3 OE 26
Array Functions

max(A) Returns the algebraically largest


element in A if A is a vector.

Returns a row vector containing the


largest elements in each column if A
is a matrix.

9/24/2024 C3 OE 27
Array Functions

Returns the algebraically smallest


min(A)
element in A if A is a vector.

Returns a row vector containing the


smallest elements in each column if
A is a matrix.

9/24/2024 C3 OE 28
Array Functions
Returns a row vector [m n] containing
size(A) the sizes of the m x n array A.

Sorts each column of the array A in


sort(A)
ascending order and returns an array the
same size as A.

sum(A) Sums the elements in each column of the


array A and returns a row vector containing
the sums.
9/24/2024 C3 OE 29
 For example, if A=

max(A) [6,2]

min(A) [-10,-5]

size(A) [3,2]

length(A) 3
-10 -5
sort (A) 3 0
6 2
sum(A) [-1 -3]
9/24/2024 C3 OE 30
𝟏𝟑 𝟎 𝟑 𝟏𝟓
Example A = −𝟕 𝟏𝟐 𝟔 𝟎
𝟓 𝟖 −𝟗 𝟐

max(A) = 13 12 6 15

min (A) = -7 0 -9 0

sum (A) = 11 20 0 17

31
sum (A,2) = 11
6

24/09/2024 L6: Array and Matrix Functions 31


𝟏𝟑 𝟎 𝟑 𝟏𝟓
−7 0 −9 0 A = −𝟕 𝟏𝟐 𝟔 𝟎
sort (A) = 5 8 3 2 𝟓 𝟖 −𝟗 𝟐
13 12 6 15

REMEBER
13 12 6 15
sort(A, 'descend’) = 5 8 3 2
−7 0 −9 0

0 3 13 15
sort(A,2) = −7 0 6 12
−9 2 5 8

24/09/2024 L6: Array and Matrix Functions 32


𝟏𝟑 𝟎 𝟑 𝟏𝟓
A = −𝟕 𝟏𝟐 𝟔 𝟎
𝟓 𝟖 −𝟗 𝟐

15 13 3 0
sort(A,2, 'descend’) = 12 6 0 −7 REMEBER
8 5 2 −9

size(A) = 3 4

length(A) = 4

24/09/2024 L6: Array and Matrix Functions 33


Sometimes we want to initialize a matrix to have all
zero elements. The zeros command creates a
matrix of all zeros.
Typing zeros(n) creates an n × n matrix of zeros,
whereas typing zeros(m,n) creates an m × n
matrix of zeros.
Typing zeros(size(A)) creates a matrix of all
zeros having the same dimension as the matrix A.
This type of matrix can be useful for applications in
which we do not know the required dimension
ahead of time.
The syntax of the ones command is the same,
except that it creates arrays filled with ones.
9/24/2024 C3 OE 34
Generating Vectors from
functions
 zeros(m,n)
x = zeros(1,3)
m x n matrix of zeros
x =
0 0 0

 ones(m,n)
m x n matrix of ones x = ones(1,3)
x =
1 1 1
9/24/2024 C3 OE 35
Example:

9/24/2024 C3 OE 36
The identity matrix is a square matrix whose
diagonal elements are all equal to one, with the
remaining elements equal to zero.
For example, the 4 × 4 identity matrix is

1 0 0 0
0 1 0 0
I= 0 0 1 0
0 0 0 1

The functions eye(n) and eye(size(A))


create an n × n identity matrix and an identity matrix
the same size as the matrix A.
9/24/2024 C3 OE 37
Example:

C4-1 Spring 2012 38


Question: Consider the following MATLAB code snippet:

A = [3 7 2; 9 5 1; 4 6 8];
B = [1 2 3; 4 5 6; 7 8 9];
C = A + B;
D = sort(C(:), 'descend');
result = sum(D(1:5));

9/24/2024 C3 OE 39
A= B=

3 7 2 1 2 3
>> C = A + B 9 5 1 4 5 6
4 6 8 7 8 9

>> C =
4 9 5 >> sum(D(1:5))
13 10 7
11 14 17 ans =

>> D = sort(C(:), 'descend') 65


D=
17
14
13
11
10
9
7
5
4
9/24/2024 C3 OE 40
g=
6 5 4
>> g = [6 5 4 ; 2 3 1 ; 3 2 5 ; 4 0 3]
2 3 1
3 2 5
4 0 3

a- the total time to produce each product ? b- the maximum process time for each product ?
>> sum(g) >> sum(g,2)
ans =
ans =
15
15 10 13 6
10
7

c- the total time that each process will take to produce all the three products ?
>> max([g]')

ans =

6 3 5 4

9/24/2024 C4 OE 41

You might also like