SlideShare una empresa de Scribd logo
1 de 21
Arrays in Matlab
UC Berkeley
Fall 2004, E77
http://jagger.me.berkeley.edu/~pack/e77
Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike
License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to
Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
A rectangular arrangement of numbers is called
an array.
Later, we will have rectangular arrangement of
more general objects. Those will also be called
arrays.
Arrays
This is a 4-by-2 array.
It has 4 rows, and 2
columns.
The (3,1) entry is the
entry in the 3rd row,
and 1st column
Scalars, Vectors and Arrays
1-by-1 arrays are also called scalars
1-by-N arrays are also called row vectors
N-by-1 arrays are also called column vectors
Row and Column vectors are also sometimes just called vectors
In Matlab, all arrays of numbers are called double
arrays.
Creating Arrays
Horizontal and Vertical Concatenation (ie., “stacking”)
– Square brackets, [, and ] to define arrays
– Spaces (and/or commas) to separate columns
– Semi-colons to separate rows
Example
>> [ 3 4 5 ; 6 7 8 ] is the 2-by-3 array
If A and B are arrays with the same number of rows, then
>> C = [ A B ] is the array formed by stacking A “next to” B
Once constructed, C does not “know” that it came from two arrays stacked
next to one another. No partitioning information is maintained.
If A and B are arrays with the same number of columns, then
>> [ A ; B ] is the array formed by stacking A “on top of” B
So, [ [ 3 ; 6 ] [ 4 5 ; 7 8 ] ] is equal to [ 3 4 5;6 7 8 ]






8
7
6
5
4
3
Creating special arrays
ones(n,m)
–a n-by-m double array, each entry is equal to 1
zeros(n,m)
–a n-by-m double array, each entry is equal to 0
rand(n,m)
–a n-by-m double array, each entry is a random number between 0
and 1.
Examples
>> A = ones(2,3);
>> B = zeros(3,4);
>> C = rand(2,5);
: convention
The “: (colon) convention” is used to create row vectors, whose
entries are evenly spaced.
7:2:18 equals the row vector [ 7 9 11 13 15 17 ]
If F, J and L are numbers with J>0, F ≤ L, then F:J:L creates a
row vector
[ F F+J F+2*J F+3*J … F+N*J ]
where F+N*J ≤ L, and F+(N+1)*J>L
Many times, the increment is 1. Shorthand for F:1:L is F:L
F:J:L with J<0 (decrementing)
You can also decrement (ie., J<0), in which case L should be
less than F.
Therefore 6.5:-1.5:0 is the row vector
[ 6.5 5.0 3.5 2.0 0.5 ]
The SIZE command
If A is an array, then size(A) is a 1-by-2 array.
–The (1,1) entry is the number of rows of A
–The (1,2) entry is the number of columns of A
If A is an array, then
size(A,1) is the number of rows of A
size(A,2) is the number of columns of A
Example
>> A = rand(5,6);
>> B = size(A)
>> size(A,2)
Accessing single elements of a vector
If A is a vector (ie, a row or column vector), then
A(1) is its first element,
A(2) is its second element,…
Example
>> A = [ 3 4.2 -7 10.1 0.4 -3.5 ];
>> A(3)
>> Index = 5;
>> A(Index)
This syntax can be used to assign an entry of A. Recall assignment
>> VariableName = Expression
An entry of an array may also be assigned
>> VariableName(Index) = Expression
So, change the 4’th entry of A to the natural logarithm of 3.
>> A(4) = log(3);
Accessing multiple elements of a vector
A(3) refers to the 3rd entry of A. However, the index need not
be a single number.
Example: Make a 1-by-6 row vector, and access multiple
elements, giving back row vectors of various dimensions.
>> A = [ 3 4.2 -7 10.1 0.4 -3.5 ];
>> A([1 4 6]) % 1-by-3, 1st, 4th, 6th entry
>> Index = [3 2 3 5];
>> A(Index) % 1-by-4
Index should contain integers. Regardless of whether A is a
row or column, Index can be a row or a column, and Matlab
will do the same thing in both cases. The expressions below
are the same.
>> A([2 4 3])
>> A([2;4;3])
Assigning multiple elements of a vector
In an assignment to multiple entries of a vector
>> A(Index) = Expression
the right-hand side expression should be a scalar, or the same
size as the array being referenced by A(Index)
Example: Make a 1-by-6 row vector, and access multiple
elements, giving back row vectors of various dimensions.
>> A = [ 3 4.2 -7 10.1 0.4 -3.5 ];
>> A([1 4 6]) = [10 100 1000];
>> Index = [3 2 3 5];
>> A(Index) = pi;
Accessing elements and parts of arrays
If M is an array, then M(3,4) is
–the element in the (3rd row , 4th column) of M
If M is an array, then M([1 4 2],[5 6])
–is a 3-by-2 array, consisting of the entries of M from
• rows [1, 4 and 2]
• columns [5 and 6]
In an assignment
>> M(RIndex,CIndex) = Expression
the right-hand side expression should be a scalar, or the same
size as the array being referenced by M(RIndex,CIndex)
Do some examples
Name = ‘A’
Size = [2 3];
Data =
Workspace: Base
Layout in memory
The entries of a numeric array in Matlab are stored
together in memory in a specific order.
>> A = [ 3 4.2 8 ; -6.7 12 0.75 ];
represents the array
Somewhere in memory, Matlab has
3
-6.7
4.2
12
8
0.75






 75
.
0
12
7
.
6
8
2
.
4
4
.
3
RESHAPE
RESHAPE changes the size, but not the values or order of the data in
memory.
>> A = [ 3 4.2 8 ; -6.7 12 0.75 ];
>> B = reshape(A,[3 2]);
The result (in memory is)
3
-6.7
4.2
12
8
0.75
Name = ‘A’
Size = [2 3];
Data =
Name = ‘B’
Size = [3 2];
Data = 3
-6.7
4.2
12
8
0.75
while B is the array











75
.
0
2
.
4
8
7
.
6
12
3
So, A is the array






 75
.
0
12
7
.
6
8
2
.
4
4
.
3
END
Suppose A is an N-by-M array, and a reference of the form
A(RIndex,CIndex)
Any occurence of the word end in the RIndex is changed
(automatically) to N
Any occurence of the word end in the CIndex is changed
(automatically) to M
Example:
>> M = rand(4,5);
>> M(end,end)
>> M([1 end],[end-2:end])
: as a row or column index
Suppose A is an N-by-M array, and a reference of the form
A(RIndex,CIndex)
If RIndex is a single colon, :, then RIndex is changed
(automatically) to 1:N (every row)
If CIndex is a single colon, :, then CIndex is changed
(automatically) to 1:M (every column)
Example:
>> M = rand(4,5);
>> M(:,[1 3 5])
LINSPACE and LOGSPACE
>> linspace(A,B,N) is a 1-by-N row vector of evenly
spaced numbers, starting at A, and ending at B
>> logspace(A,B,N) is a 1-by-N row vector of
logarithmically spaced numbers, starting at 10A, and ending at
10B.
Examples
>> linspace(1,4,6)
>> logspace(-1,1,5)
>> log10(logspace(-1,1,5))
Unary Numeric Operations on double Arrays
Unary operations involve one input argument. Examples are:
–Negation, using the “minus” sign
–Trig functions, sin, cos, tan, asin, acos, atan,…
–General rounding functions, floor, ceil, fix, round
–Exponential and logs, exp, log, log10, sqrt
–Complex, abs, angle, real, imag
Example: If A is an N1-by-N2-by-N3-by-… array, then
B = sin(A);
is an N1-by-N2-by-N3-by-… array. Every entry of B is
the sin of the corresponding entry of A. The “for”-loop that
cycles the calculation over all array entries is an example of the
vectorized nature of many Matlab builtin functions
Binary (two arguments) operations on Arrays
Addition (and subtraction)
– If A and B are arrays of the same size, then A+B is an array of the same size
whose individual entries are the sum of the corresponding entries of A and B
– If A is an array and B is a scalar, then A+B is an array of the same size as A,
whose individual entries are the sum of the corresponding entries of A and the
scalar B
– If A is a scalar, and B is an array, use same logic as above
Scalar-Array Multiplication
– If A is an array,and B is a scalar, then A*B is an array of the same size as A,
whose individual entries are the product of the corresponding entries of A and
the scalar B.
Element-by-Element Multiplication
– If A and B are arrays of the same size, then A.*B is an array of the same size
whose individual entries are the product of the corresponding entries of A and
B
Matrix multiplication
– If A and B are arrays, then A*B is the matrix multiplication of the two arrays…
More later
Intro to plotting with Matlab
If X is a 1-by-N (or N-by-1) vector, and Y is a 1-by-N (or N-by-1)
vector, then
>> plot(X,Y)
creates a figure window, and plots the data in the axis. The points plotted
are
(X(1),Y(1)), (X(2),Y(2)), … , (X(N),Y(N)).
By default, Matlab will draw straight lines between the data points, and the
points will not be explicitly marked. For more info, do >> help plot
Example:
>> X = linspace(0,3*pi,1000);
>> Y = sin(X);
>> plot(X,Y)
Plotting several lines
If X1 and Y1 are both 1-by-N, and X2 and Y2 are both 1-
by-M, then
>> plot(X1,Y1,X2,Y2)
will plot both sets of data on the same axis.
Example
>> X1 = linspace(0,pi,1000);
>> Y1 = cos(4*X1).*sin(X1);
>> X2 = [0 1 4 5];
>> plot(X1,Y1,X2,sqrt(X2))

Más contenido relacionado

Similar a ArrayBasics.ppt

2.Exploration with CAS-I.Lab2.pptx
2.Exploration with CAS-I.Lab2.pptx2.Exploration with CAS-I.Lab2.pptx
2.Exploration with CAS-I.Lab2.pptxakshatraj875
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
Advanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & ScientistsAdvanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & ScientistsRay Phan
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data StructureMandeep Singh
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd yearpalhimanshi999
 
02 linear algebra
02 linear algebra02 linear algebra
02 linear algebraRonald Teo
 
Matrices, Arrays and Vectors in MATLAB
Matrices, Arrays and Vectors in MATLABMatrices, Arrays and Vectors in MATLAB
Matrices, Arrays and Vectors in MATLABAbu Raihan Ibna Ali
 
Digital communication lab lectures
Digital communication lab  lecturesDigital communication lab  lectures
Digital communication lab lecturesmarwaeng
 
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdfIntroduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdfYasirMuhammadlawan
 
Bba i-bm-u-2- matrix -
Bba i-bm-u-2- matrix -Bba i-bm-u-2- matrix -
Bba i-bm-u-2- matrix -Rai University
 

Similar a ArrayBasics.ppt (20)

2.Exploration with CAS-I.Lab2.pptx
2.Exploration with CAS-I.Lab2.pptx2.Exploration with CAS-I.Lab2.pptx
2.Exploration with CAS-I.Lab2.pptx
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
cluod.pdf
cluod.pdfcluod.pdf
cluod.pdf
 
Arrays
ArraysArrays
Arrays
 
1
11
1
 
Advanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & ScientistsAdvanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & Scientists
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd year
 
02 linear algebra
02 linear algebra02 linear algebra
02 linear algebra
 
02 linear algebra
02 linear algebra02 linear algebra
02 linear algebra
 
Comp102 lec 8
Comp102   lec 8Comp102   lec 8
Comp102 lec 8
 
Matrices, Arrays and Vectors in MATLAB
Matrices, Arrays and Vectors in MATLABMatrices, Arrays and Vectors in MATLAB
Matrices, Arrays and Vectors in MATLAB
 
Matrices
MatricesMatrices
Matrices
 
Lecture two
Lecture twoLecture two
Lecture two
 
Digital communication lab lectures
Digital communication lab  lecturesDigital communication lab  lectures
Digital communication lab lectures
 
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdfIntroduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
 
Bba i-bm-u-2- matrix -
Bba i-bm-u-2- matrix -Bba i-bm-u-2- matrix -
Bba i-bm-u-2- matrix -
 
Matlab cheatsheet
Matlab cheatsheetMatlab cheatsheet
Matlab cheatsheet
 

Último

DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projectssmsksolar
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfsmsksolar
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network DevicesChandrakantDivate1
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stageAbc194748
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...soginsider
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesRAJNEESHKUMAR341697
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 

Último (20)

DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stage
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 

ArrayBasics.ppt

  • 1. Arrays in Matlab UC Berkeley Fall 2004, E77 http://jagger.me.berkeley.edu/~pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  • 2. A rectangular arrangement of numbers is called an array. Later, we will have rectangular arrangement of more general objects. Those will also be called arrays. Arrays This is a 4-by-2 array. It has 4 rows, and 2 columns. The (3,1) entry is the entry in the 3rd row, and 1st column
  • 3. Scalars, Vectors and Arrays 1-by-1 arrays are also called scalars 1-by-N arrays are also called row vectors N-by-1 arrays are also called column vectors Row and Column vectors are also sometimes just called vectors In Matlab, all arrays of numbers are called double arrays.
  • 4. Creating Arrays Horizontal and Vertical Concatenation (ie., “stacking”) – Square brackets, [, and ] to define arrays – Spaces (and/or commas) to separate columns – Semi-colons to separate rows Example >> [ 3 4 5 ; 6 7 8 ] is the 2-by-3 array If A and B are arrays with the same number of rows, then >> C = [ A B ] is the array formed by stacking A “next to” B Once constructed, C does not “know” that it came from two arrays stacked next to one another. No partitioning information is maintained. If A and B are arrays with the same number of columns, then >> [ A ; B ] is the array formed by stacking A “on top of” B So, [ [ 3 ; 6 ] [ 4 5 ; 7 8 ] ] is equal to [ 3 4 5;6 7 8 ]       8 7 6 5 4 3
  • 5. Creating special arrays ones(n,m) –a n-by-m double array, each entry is equal to 1 zeros(n,m) –a n-by-m double array, each entry is equal to 0 rand(n,m) –a n-by-m double array, each entry is a random number between 0 and 1. Examples >> A = ones(2,3); >> B = zeros(3,4); >> C = rand(2,5);
  • 6. : convention The “: (colon) convention” is used to create row vectors, whose entries are evenly spaced. 7:2:18 equals the row vector [ 7 9 11 13 15 17 ] If F, J and L are numbers with J>0, F ≤ L, then F:J:L creates a row vector [ F F+J F+2*J F+3*J … F+N*J ] where F+N*J ≤ L, and F+(N+1)*J>L Many times, the increment is 1. Shorthand for F:1:L is F:L
  • 7. F:J:L with J<0 (decrementing) You can also decrement (ie., J<0), in which case L should be less than F. Therefore 6.5:-1.5:0 is the row vector [ 6.5 5.0 3.5 2.0 0.5 ]
  • 8. The SIZE command If A is an array, then size(A) is a 1-by-2 array. –The (1,1) entry is the number of rows of A –The (1,2) entry is the number of columns of A If A is an array, then size(A,1) is the number of rows of A size(A,2) is the number of columns of A Example >> A = rand(5,6); >> B = size(A) >> size(A,2)
  • 9. Accessing single elements of a vector If A is a vector (ie, a row or column vector), then A(1) is its first element, A(2) is its second element,… Example >> A = [ 3 4.2 -7 10.1 0.4 -3.5 ]; >> A(3) >> Index = 5; >> A(Index) This syntax can be used to assign an entry of A. Recall assignment >> VariableName = Expression An entry of an array may also be assigned >> VariableName(Index) = Expression So, change the 4’th entry of A to the natural logarithm of 3. >> A(4) = log(3);
  • 10. Accessing multiple elements of a vector A(3) refers to the 3rd entry of A. However, the index need not be a single number. Example: Make a 1-by-6 row vector, and access multiple elements, giving back row vectors of various dimensions. >> A = [ 3 4.2 -7 10.1 0.4 -3.5 ]; >> A([1 4 6]) % 1-by-3, 1st, 4th, 6th entry >> Index = [3 2 3 5]; >> A(Index) % 1-by-4 Index should contain integers. Regardless of whether A is a row or column, Index can be a row or a column, and Matlab will do the same thing in both cases. The expressions below are the same. >> A([2 4 3]) >> A([2;4;3])
  • 11. Assigning multiple elements of a vector In an assignment to multiple entries of a vector >> A(Index) = Expression the right-hand side expression should be a scalar, or the same size as the array being referenced by A(Index) Example: Make a 1-by-6 row vector, and access multiple elements, giving back row vectors of various dimensions. >> A = [ 3 4.2 -7 10.1 0.4 -3.5 ]; >> A([1 4 6]) = [10 100 1000]; >> Index = [3 2 3 5]; >> A(Index) = pi;
  • 12. Accessing elements and parts of arrays If M is an array, then M(3,4) is –the element in the (3rd row , 4th column) of M If M is an array, then M([1 4 2],[5 6]) –is a 3-by-2 array, consisting of the entries of M from • rows [1, 4 and 2] • columns [5 and 6] In an assignment >> M(RIndex,CIndex) = Expression the right-hand side expression should be a scalar, or the same size as the array being referenced by M(RIndex,CIndex) Do some examples
  • 13. Name = ‘A’ Size = [2 3]; Data = Workspace: Base Layout in memory The entries of a numeric array in Matlab are stored together in memory in a specific order. >> A = [ 3 4.2 8 ; -6.7 12 0.75 ]; represents the array Somewhere in memory, Matlab has 3 -6.7 4.2 12 8 0.75        75 . 0 12 7 . 6 8 2 . 4 4 . 3
  • 14. RESHAPE RESHAPE changes the size, but not the values or order of the data in memory. >> A = [ 3 4.2 8 ; -6.7 12 0.75 ]; >> B = reshape(A,[3 2]); The result (in memory is) 3 -6.7 4.2 12 8 0.75 Name = ‘A’ Size = [2 3]; Data = Name = ‘B’ Size = [3 2]; Data = 3 -6.7 4.2 12 8 0.75 while B is the array            75 . 0 2 . 4 8 7 . 6 12 3 So, A is the array        75 . 0 12 7 . 6 8 2 . 4 4 . 3
  • 15. END Suppose A is an N-by-M array, and a reference of the form A(RIndex,CIndex) Any occurence of the word end in the RIndex is changed (automatically) to N Any occurence of the word end in the CIndex is changed (automatically) to M Example: >> M = rand(4,5); >> M(end,end) >> M([1 end],[end-2:end])
  • 16. : as a row or column index Suppose A is an N-by-M array, and a reference of the form A(RIndex,CIndex) If RIndex is a single colon, :, then RIndex is changed (automatically) to 1:N (every row) If CIndex is a single colon, :, then CIndex is changed (automatically) to 1:M (every column) Example: >> M = rand(4,5); >> M(:,[1 3 5])
  • 17. LINSPACE and LOGSPACE >> linspace(A,B,N) is a 1-by-N row vector of evenly spaced numbers, starting at A, and ending at B >> logspace(A,B,N) is a 1-by-N row vector of logarithmically spaced numbers, starting at 10A, and ending at 10B. Examples >> linspace(1,4,6) >> logspace(-1,1,5) >> log10(logspace(-1,1,5))
  • 18. Unary Numeric Operations on double Arrays Unary operations involve one input argument. Examples are: –Negation, using the “minus” sign –Trig functions, sin, cos, tan, asin, acos, atan,… –General rounding functions, floor, ceil, fix, round –Exponential and logs, exp, log, log10, sqrt –Complex, abs, angle, real, imag Example: If A is an N1-by-N2-by-N3-by-… array, then B = sin(A); is an N1-by-N2-by-N3-by-… array. Every entry of B is the sin of the corresponding entry of A. The “for”-loop that cycles the calculation over all array entries is an example of the vectorized nature of many Matlab builtin functions
  • 19. Binary (two arguments) operations on Arrays Addition (and subtraction) – If A and B are arrays of the same size, then A+B is an array of the same size whose individual entries are the sum of the corresponding entries of A and B – If A is an array and B is a scalar, then A+B is an array of the same size as A, whose individual entries are the sum of the corresponding entries of A and the scalar B – If A is a scalar, and B is an array, use same logic as above Scalar-Array Multiplication – If A is an array,and B is a scalar, then A*B is an array of the same size as A, whose individual entries are the product of the corresponding entries of A and the scalar B. Element-by-Element Multiplication – If A and B are arrays of the same size, then A.*B is an array of the same size whose individual entries are the product of the corresponding entries of A and B Matrix multiplication – If A and B are arrays, then A*B is the matrix multiplication of the two arrays… More later
  • 20. Intro to plotting with Matlab If X is a 1-by-N (or N-by-1) vector, and Y is a 1-by-N (or N-by-1) vector, then >> plot(X,Y) creates a figure window, and plots the data in the axis. The points plotted are (X(1),Y(1)), (X(2),Y(2)), … , (X(N),Y(N)). By default, Matlab will draw straight lines between the data points, and the points will not be explicitly marked. For more info, do >> help plot Example: >> X = linspace(0,3*pi,1000); >> Y = sin(X); >> plot(X,Y)
  • 21. Plotting several lines If X1 and Y1 are both 1-by-N, and X2 and Y2 are both 1- by-M, then >> plot(X1,Y1,X2,Y2) will plot both sets of data on the same axis. Example >> X1 = linspace(0,pi,1000); >> Y1 = cos(4*X1).*sin(X1); >> X2 = [0 1 4 5]; >> plot(X1,Y1,X2,sqrt(X2))