Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

CL2014 - MATLAB Programming - Lec03

The document discusses matrices and arrays in MATLAB. Some key points: - Matrices and vectors are special types of arrays - Arrays can be one-dimensional (vectors) or two-dimensional (matrices) - Elements of arrays and matrices can be accessed and operated on - Vectors can be generated with constant spacing using colon operator - Matrix operations include transpose, inverse, addition, multiplication

Uploaded by

Umar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CL2014 - MATLAB Programming - Lec03

The document discusses matrices and arrays in MATLAB. Some key points: - Matrices and vectors are special types of arrays - Arrays can be one-dimensional (vectors) or two-dimensional (matrices) - Elements of arrays and matrices can be accessed and operated on - Vectors can be generated with constant spacing using colon operator - Matrix operations include transpose, inverse, addition, multiplication

Uploaded by

Umar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CL2014

MATLAB Programming
Lecture: 03

Course Instructor
Engr Muhammad Umar Khan Niazi
Office: C1-21, Ext.: 218, Email: umar.niazi@nu.edu.pk
1
2 Matrices & Arrays

 A vector is a special type of matrix, having only one row or one column.
Vectors are called lists or arrays in other programming languages.
 MATLAB refers to scalars, vectors, and matrices generally as arrays.
 We will also use the term array generally, with vector and matrix
referring to the one-dimensional (1D) and two-dimensional (2D) array
3 Matrices & Arrays

 An array is Basic data structure in MATLAB.


 In MATLAB, each type of data is stored in arrays.
 Therefore, working in arrays is fundamental to working in MATLAB.

 In MATLAB, it is easy to…


Create arrays
Assign/access values to each element of an array.
Perform different operation on the contents of an array
4 Matrices & Arrays

 A matrix may be thought of as a table consisting of rows and columns.


You create a matrix just as you do a vector, except that a semicolon is
used to indicate the end of a row. For example, the statement
a = [1 2 3; 4 5 6]
results in
a =
1
2 3
4
5 6
5 Matrices & Arrays

 In 1D array, the data is in a single row or in a single column.


Example:

NOTE:
• Array is stored by a specific name.
• The arrays are written in square brackets [ ].
• Elements of a row are separated by space.

• Any element of an can be retrieved by using its address


6 Matrices & Arrays

 In 1D array, the data is in a single row or in a single column.


Example (Row Example (Column Vector):
Vector):
7 Vectors with Constant Spacing
A vector with constant spacing can be generated by using colon ( : )
Array = m : q : n

‘m’ is first number

‘n’ is last number

‘q’ is step, i.e. difference between consecutive numbers

If ‘q’ is omitted then step size will be 1 ArrayB = m : n


8 Vectors with Constant Spacing
9 Linearly Spaced Numbers

The function ‘linspace’ can be used to initialize a vector of equally spaced


values:

y = linspace(0, pi/2, 10)


y = linspace(x1,x2,n)

y = linspace(x 1 ,x 2 ) Generates 100 linearly spaced numbers between x 1 and x 2 y =


linspace(x 1 ,x 2 ,n) Generates n linearly spaced numbers between x 1 and x 2
10 Linearly Spaced Numbers
11 Linearly Spaced Numbers
12 Matrix Operations in MATLAB

A matrix may be thought of as a table consisting of rows and columns. You


create a matrix just as you do a vector, except that a semicolon is used to
indicate the end of a row. For example, the statement

a = [1 2 3; 4 5 6]

Results In
a =
1 2 3
4 5 6
13 Matrix Operations in MATLAB

A matrix may be transposed: With a initialized as above, the statement a’


results in

ans =
1 4
2 5
3 6
14 Matrix Operations in MATLAB

A matrix can be constructed from column vectors of the same length. Thus,
the statements

x = 0:30:180;
table = [x’ sin(x*pi/180)’]

result in
table =
0 0
30.0000 0.5000
60.0000 0.8660
90.0000 1.0000
120.0000 0.8660
150.0000 0.5000
180.0000 0.0000
15 Matrix Operations in MATLAB

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

n*A or A*n multiplies each element of A by n


Command Return
a’ Transpose of a
find(a) Indices of all non-zero elements in a.
fliplr(a) Matrix a, flipped horizontally
flipud(a) Matrix a, flipped vertically.
inv(a) Inverse of a
min(a) Minimum-valued element of a. † † For a matrix, the
max(a) Maximum-valued element of a. † operation will be
carried
numel(a) The number of elements of a.
out separately on
repmat(a,m,n) A matrix where matrix a is repeated in m rows and n columns column.
each For a
reshape(a,m,n) Matrix a reshaped into m rows and n columns. vector (row or
column), the
size(a) The size of a (#rows, #columns, ...)
operation will be
sort(a) Vector a sorted into ascending order. † carried out on the
sum(a) Sum of elements of a. † vector.
unique(a) The list of unique elements of a in ascending order. 1
6

You might also like