SlideShare a Scribd company logo
1 of 16
Download to read offline
MATRICES IN MATLAB
Ritesh Kumar
Defining matrices in MATLAB
� 1D row vector – [1 2 3 4] or [1,2,3,4]
� 1D column vector – [1;2;3;4]
� 2D matrix – [1 2 3; 4 5 6; 7 8 9]
� Make a 1D row vector from 1 to 100. Tired ?
Use this – [1:1:100] or [1:100].
Try changing the step size.
� ‘:’ means ‘to’
Using Colon (:)
� [1:4] gives a row vector containing [1 2 3 4]
� [1:0.5:4] gives a row vector from 1 to 4 with a
step size of 0.5
� Make a vector from 0 to 2π with a step size of
0.01
Matrix operators
� Normal operations
� A + B
� A – B
� A*B
� A^2
� Dot (.) operations: Element wise operations
� Y = A.^2
� Y = A.*B
� Y = A.^B
Predefined functions for Matrices
� Mathematical
�sin, cos, tan
�log, exp
�sqrt
� Creation
�ones
�zeros
�eye
�rand
� Information
�size
�length
Predefined functions for Matrices
� Matrix operations
� sum
� diag
� transpose or ‘
� inv
� det
� eig
� fliplr
� reshape
� flipud
� rot90
� repmat
Matrix concatenation
Suppose A, B, C are matrices
� If we write [A B] or [A,B]
� If we write [A;B]
� Similarly [A B C] and [A;B;C]
� [[A B];C]
Exercise
� Create a vector of 20 elements in GP with first
term 1 and common ratio ½. Calculate its sum
� Calculate the sum of first 15 elements of the
series
Indexing
� Extracting elements from the arrays
� Normal Indexing
� Linear Indexing
� Logical Indexing
Array indices
� A is an n×m matrix
� To extract the 5th row 6th column element
we’ll write A(5,6)
� The difference between Y=A(5,6) and
A(5,6)=Y
� Y=A(5,6) will extract the 5,6 element from A and
save it to Y
� A(5,6)=Y will change the 5,6 element to Y
Array indices
� What if we wish to extract multiple columns or
rows?
� A([1 3 7],[1 2])
� Gives a sub-matrix containing elements which were in
1,3 and 7th row and 1 and 2 columns of matrix A
� A([3 2 7],[3 2 3])
� A(:,[5 6])
� Here : means all
� Gives a sub-matrix containing all rows and 5 and 6
columns of matrix A
Linear Indexing
� A is 2D but we can use a single index to
extract any element from A
� A(5)
� Same as A(2,2)
A= 2 3 2
3 1 2
6 9 10
Exercise
� Extract a diagonal of a square 5x5 matrix
using linear indexing without using inbuilt
command.
Logical indexing
� Index with a matrix of 0 & 1
� Return those elements which corresponds to 1
� B=A>0;
� C=A(B);
Exercise
� Create a random matrix of 1x100. Change the
elements to 0 which are smaller than 0.33 and
1 which are greater than 0.33 and smaller than
0.67 and 2 which are greater than 0.67
Exercise
� A=[1 2 0 6 4 0 2];
� Get a B vector such that B=[1 ½ 0 1/6 ¼ 0 ½],
i.e. reverse the elements which are not 0

More Related Content

Similar to Basics of Matlab for students and faculty

COMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptxCOMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptximman gwu
 
INTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxINTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxDevaraj Chilakala
 
sparse matrices in tress
sparse matrices in tresssparse matrices in tress
sparse matrices in tresspavialone
 
Notes on MATLAB - Basic Cheatsheet
Notes on MATLAB    -    Basic CheatsheetNotes on MATLAB    -    Basic Cheatsheet
Notes on MATLAB - Basic CheatsheetAhmed Nassar
 
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
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functionsjoellivz
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programmingDamian T. Gordon
 
Matlab level 1.pptx
Matlab level 1.pptxMatlab level 1.pptx
Matlab level 1.pptxAbanobGozef
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.pptkebeAman
 

Similar to Basics of Matlab for students and faculty (20)

Mat lab
Mat labMat lab
Mat lab
 
COMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptxCOMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptx
 
Matlab
MatlabMatlab
Matlab
 
MATLAB ARRAYS
MATLAB ARRAYSMATLAB ARRAYS
MATLAB ARRAYS
 
INTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxINTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptx
 
presentation.pptx
presentation.pptxpresentation.pptx
presentation.pptx
 
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
 
Basic concepts in_matlab
Basic concepts in_matlabBasic concepts in_matlab
Basic concepts in_matlab
 
sparse matrices in tress
sparse matrices in tresssparse matrices in tress
sparse matrices in tress
 
Mat lab day 1
Mat lab day 1Mat lab day 1
Mat lab day 1
 
Notes on MATLAB - Basic Cheatsheet
Notes on MATLAB    -    Basic CheatsheetNotes on MATLAB    -    Basic Cheatsheet
Notes on MATLAB - Basic Cheatsheet
 
bobok
bobokbobok
bobok
 
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
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
 
Matlab level 1.pptx
Matlab level 1.pptxMatlab level 1.pptx
Matlab level 1.pptx
 
EPE821_Lecture3.pptx
EPE821_Lecture3.pptxEPE821_Lecture3.pptx
EPE821_Lecture3.pptx
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.ppt
 

Recently uploaded

Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Recently uploaded (20)

(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

Basics of Matlab for students and faculty

  • 2. Defining matrices in MATLAB � 1D row vector – [1 2 3 4] or [1,2,3,4] � 1D column vector – [1;2;3;4] � 2D matrix – [1 2 3; 4 5 6; 7 8 9] � Make a 1D row vector from 1 to 100. Tired ? Use this – [1:1:100] or [1:100]. Try changing the step size. � ‘:’ means ‘to’
  • 3. Using Colon (:) � [1:4] gives a row vector containing [1 2 3 4] � [1:0.5:4] gives a row vector from 1 to 4 with a step size of 0.5 � Make a vector from 0 to 2π with a step size of 0.01
  • 4. Matrix operators � Normal operations � A + B � A – B � A*B � A^2 � Dot (.) operations: Element wise operations � Y = A.^2 � Y = A.*B � Y = A.^B
  • 5. Predefined functions for Matrices � Mathematical �sin, cos, tan �log, exp �sqrt � Creation �ones �zeros �eye �rand � Information �size �length
  • 6. Predefined functions for Matrices � Matrix operations � sum � diag � transpose or ‘ � inv � det � eig � fliplr � reshape � flipud � rot90 � repmat
  • 7. Matrix concatenation Suppose A, B, C are matrices � If we write [A B] or [A,B] � If we write [A;B] � Similarly [A B C] and [A;B;C] � [[A B];C]
  • 8. Exercise � Create a vector of 20 elements in GP with first term 1 and common ratio ½. Calculate its sum � Calculate the sum of first 15 elements of the series
  • 9. Indexing � Extracting elements from the arrays � Normal Indexing � Linear Indexing � Logical Indexing
  • 10. Array indices � A is an n×m matrix � To extract the 5th row 6th column element we’ll write A(5,6) � The difference between Y=A(5,6) and A(5,6)=Y � Y=A(5,6) will extract the 5,6 element from A and save it to Y � A(5,6)=Y will change the 5,6 element to Y
  • 11. Array indices � What if we wish to extract multiple columns or rows? � A([1 3 7],[1 2]) � Gives a sub-matrix containing elements which were in 1,3 and 7th row and 1 and 2 columns of matrix A � A([3 2 7],[3 2 3]) � A(:,[5 6]) � Here : means all � Gives a sub-matrix containing all rows and 5 and 6 columns of matrix A
  • 12. Linear Indexing � A is 2D but we can use a single index to extract any element from A � A(5) � Same as A(2,2) A= 2 3 2 3 1 2 6 9 10
  • 13. Exercise � Extract a diagonal of a square 5x5 matrix using linear indexing without using inbuilt command.
  • 14. Logical indexing � Index with a matrix of 0 & 1 � Return those elements which corresponds to 1 � B=A>0; � C=A(B);
  • 15. Exercise � Create a random matrix of 1x100. Change the elements to 0 which are smaller than 0.33 and 1 which are greater than 0.33 and smaller than 0.67 and 2 which are greater than 0.67
  • 16. Exercise � A=[1 2 0 6 4 0 2]; � Get a B vector such that B=[1 ½ 0 1/6 ¼ 0 ½], i.e. reverse the elements which are not 0