SlideShare a Scribd company logo
8
Most read
10
Most read
16
Most read
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

PPTX
MATLAB - Arrays and Matrices
PPT
Matlab Introduction Simulink and Basics ds
PPTX
Matlab matrices and arrays
PPTX
Chap1 Matrix and Linear Algerbra.s,jspptx
PPTX
1. Introduction to Computing - MATLAB.pptx
PDF
Matrix Indexing in MATLAB - MATLAB & Simulink.pdf
PPTX
1. Introduction.pptx
PPTX
Interactive Computation With MATLAB.pptx
MATLAB - Arrays and Matrices
Matlab Introduction Simulink and Basics ds
Matlab matrices and arrays
Chap1 Matrix and Linear Algerbra.s,jspptx
1. Introduction to Computing - MATLAB.pptx
Matrix Indexing in MATLAB - MATLAB & Simulink.pdf
1. Introduction.pptx
Interactive Computation With MATLAB.pptx

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

PPTX
5_Vectors & Matrices for Engineers .pptx
PPT
Introduction to Matlab - Basic Functions
PPTX
Importance of matlab
PPTX
Introduction to MATLAB Programming for Engineers
PPTX
TRAINING PROGRAMME ON MATLAB ASSOCIATE EXAM (1).pptx
PPT
Matlab introduction
PDF
PPT
Introduction to MatLab programming
PDF
Basic concepts in_matlab
PDF
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
PDF
PPT
Matlab introduction
PDF
Matlab Intro to Arrays Guide for Beginners.pdf
PPS
PPTX
Basic MATLAB-Presentation.pptx
PDF
Matlab cheatsheet
PDF
Commands list
PPTX
Introduction to matlab lecture 2 of 4
DOC
Matlab tut3
PDF
Basics of matlab
5_Vectors & Matrices for Engineers .pptx
Introduction to Matlab - Basic Functions
Importance of matlab
Introduction to MATLAB Programming for Engineers
TRAINING PROGRAMME ON MATLAB ASSOCIATE EXAM (1).pptx
Matlab introduction
Introduction to MatLab programming
Basic concepts in_matlab
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
Matlab introduction
Matlab Intro to Arrays Guide for Beginners.pdf
Basic MATLAB-Presentation.pptx
Matlab cheatsheet
Commands list
Introduction to matlab lecture 2 of 4
Matlab tut3
Basics of matlab
Ad

Recently uploaded (20)

PPTX
Welding lecture in detail for understanding
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Geodesy 1.pptx...............................................
PPT
Project quality management in manufacturing
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
web development for engineering and engineering
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
additive manufacturing of ss316l using mig welding
Welding lecture in detail for understanding
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
UNIT 4 Total Quality Management .pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
bas. eng. economics group 4 presentation 1.pptx
Geodesy 1.pptx...............................................
Project quality management in manufacturing
Model Code of Practice - Construction Work - 21102022 .pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Automation-in-Manufacturing-Chapter-Introduction.pdf
Internet of Things (IOT) - A guide to understanding
web development for engineering and engineering
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
additive manufacturing of ss316l using mig welding
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