SlideShare a Scribd company logo
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
Ad

More Related Content

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

Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
Mohd Esa
 
Mat lab
Mat labMat lab
Mat lab
Gizachew Kefelew
 
COMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptxCOMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptx
imman gwu
 
Matlab
MatlabMatlab
Matlab
Sri Chakra Kumar
 
MATLAB ARRAYS
MATLAB ARRAYSMATLAB ARRAYS
MATLAB ARRAYS
Aditya Choudhury
 
INTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxINTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptx
Devaraj Chilakala
 
presentation.pptx
presentation.pptxpresentation.pptx
presentation.pptx
raghav415187
 
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
Shameer Ahmed Koya
 
Basic concepts in_matlab
Basic concepts in_matlabBasic concepts in_matlab
Basic concepts in_matlab
Mohammad Alauddin
 
sparse matrices in tress
sparse matrices in tresssparse matrices in tress
sparse matrices in tress
pavialone
 
Mat lab day 1
Mat lab day 1Mat lab day 1
Mat lab day 1
Kassandra Kay Mislang
 
Notes on MATLAB - Basic Cheatsheet
Notes on MATLAB    -    Basic CheatsheetNotes on MATLAB    -    Basic Cheatsheet
Notes on MATLAB - Basic Cheatsheet
Ahmed Nassar
 
bobok
bobokbobok
bobok
Adi Pandarangga
 
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
akshatraj875
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
joellivz
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
Damian T. Gordon
 
473431331-Matlab-Simulink-Tutorial-ppt.ppt
473431331-Matlab-Simulink-Tutorial-ppt.ppt473431331-Matlab-Simulink-Tutorial-ppt.ppt
473431331-Matlab-Simulink-Tutorial-ppt.ppt
Gopal Mahato
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
Damian T. Gordon
 
Matlab level 1.pptx
Matlab level 1.pptxMatlab level 1.pptx
Matlab level 1.pptx
AbanobGozef
 
EPE821_Lecture3.pptx
EPE821_Lecture3.pptxEPE821_Lecture3.pptx
EPE821_Lecture3.pptx
Ihtisham Uddin
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
Mohd Esa
 
COMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptxCOMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptx
imman gwu
 
INTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxINTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptx
Devaraj Chilakala
 
sparse matrices in tress
sparse matrices in tresssparse matrices in tress
sparse matrices in tress
pavialone
 
Notes on MATLAB - Basic Cheatsheet
Notes on MATLAB    -    Basic CheatsheetNotes on MATLAB    -    Basic Cheatsheet
Notes on MATLAB - Basic Cheatsheet
Ahmed 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.pptx
akshatraj875
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
joellivz
 
473431331-Matlab-Simulink-Tutorial-ppt.ppt
473431331-Matlab-Simulink-Tutorial-ppt.ppt473431331-Matlab-Simulink-Tutorial-ppt.ppt
473431331-Matlab-Simulink-Tutorial-ppt.ppt
Gopal Mahato
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
Damian T. Gordon
 
Matlab level 1.pptx
Matlab level 1.pptxMatlab level 1.pptx
Matlab level 1.pptx
AbanobGozef
 

Recently uploaded (20)

Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
AI Publications
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Journal of Soft Computing in Civil Engineering
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic AlgorithmDesign Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Journal of Soft Computing in Civil Engineering
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
AI Publications
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Ad

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
  翻译: