SlideShare a Scribd company logo
Lab No.02

M-files and Matrix operations

Designed by : Dawar Awan
dawar@cecos.edu.pk
CECOS College of Engineering and IT

March – July 2012
Matrices
 MATLAB treats every thing as a matrix (check the Workspace)
 1-by-1 matrices are interpreted as scalars
 Matrices with only one row or one column are known as vectors

 A matrix is a rectangular array of numbers.
1 9 2 6
7 8 3 5
1 8 2 4
A matrix with 3 rows, 4 columns, and 12 elements

CECOS College of Engineering and IT

March – July 2012
Defining matrices
>> A = [1 2 3; 4 5 6; 7 8 9];
>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];

and
>> A = [ 1 2 3
456
7 8 9 ];
Defines

A=
1

3

4

5

6

7
CECOS College of Engineering and IT

2

8

9
March – July 2012
Accessing matrix elements
 The matrix element located in the i-th row and j-th column of
“A” is referred to as, A(i,j)

 Try the following instruction and observe the change in “A”
>> A(2,3) = 10

CECOS College of Engineering and IT

March – July 2012
Some Built-in matrix functions
Function Description
diag
eye
magic
ones
rand
triu
tril
zeros
size
length
find

:
:
:
:
:
:
:
:
:
:
:

CECOS College of Engineering and IT

returns diagonal elements as vector
identity matrix
magic square
matrix of ones
randomly generated matrix
upper triangular part of a matrix
lower triangular part of a matrix
matrix of zeros
Number of rows and columns in a matrix
Length of an array
find indices of some elements in array

March – July 2012
Matrix functions : Examples
>> a=[1 2 3;4 5 6;7 8 9]
a=
1
4
7

2
5
8

3
6
9

>> diag(a)

>> triu(a)

>> tril(a)

>> size(a)

>> length(a)

ans =

ans =

ans =

ans =

ans =

1
5
9
CECOS College of Engineering and IT

1
0
0

2
5
0

3
6
9

1
4
7

0
5
8

0
0
9

3

3

3

March – July 2012
Matrix functions : Examples
>> eye(3)

>> magic(3)

>> rand(3)

ans =

ans =

ans =

1
0
0

0
1
0

0
0
1

8
3
4

1
5
9

6
7
2

0.8147 0.9134 0.2785
0.9058 0.6324 0.5469
0.1270 0.0975 0.9575

>> ones(3)

>> ones(3,2)

>> zeros(3)

>> zeros(3,2)

ans =

ans =

ans =

ans =

1
1
1

1
1
1

1
1
1

CECOS College of Engineering and IT

1
1
1

1
1
1

0
0
0

0
0
0

0
0
0

0
0
0

0
0
0
March – July 2012
Matrix functions : Examples
>> rand(3,2)

ans =
0.9649 0.9572
0.1576 0.4854
0.9706 0.8003

CECOS College of Engineering and IT

March – July 2012
Matrix operations
+
*
^
'
/

=>
=>
=>
=>
=>
=>

Addition
Subtraction
Multiplication
Power
Conjugate transpose
Division

 To perform index by index operation, use dot (.)
notation
./
.*
.^
.'

=>
=>
=>
=>

CECOS College of Engineering and IT

Index by index division
Index by index multiplication
Index by index power
Non conjugate transpose
March – July 2012
Dot notation
 Observe the change in result due to the “dot”

CECOS College of Engineering and IT

March – July 2012
Examples/Exercise
1
4
7

C=

B=

A=
2
5
8

3
6
9

1
2
3

1
2
3

1
2
3

1

D=
2

1

2

2

1

2

1

 Practice the following matrix operations
A+B, A’, A*B, 2*A, A/2, A/B, A./B, C/D, C./D, C*D, C.*D, C^2, C.^2

CECOS College of Engineering and IT

March – July 2012
Colon notation

CECOS College of Engineering and IT

March – July 2012
M - files
 M-files are script/text files containing MATLAB commands
Script M-file
Function M-file

 To make a script M-file, you need to open a file using the built-in
MATLAB editor.
 There are two ways to accomplish it:
1. From file menu, click NEW
2. Type edit on command line
CECOS College of Engineering and IT

March – July 2012
M - files

 To save the M-file go to the “file” menu and click on “save”.
 The name should not contain spaces, should not start with a number and should not
be same as any built-in function.

 The extension of this file should be “.m”
CECOS College of Engineering and IT

March – July 2012
M - files
 This code can now be executed in two ways
1. Write the name of file on command window
(excluding “.m”)
2. Under the “debug” menu, click “Run”
 The variables used in the M-file will be maintained in the workspace

and hence accessible from the command window.

CECOS College of Engineering and IT

March – July 2012
M - files
 Code written in M-file to calculate various parameters of a circle

 Check the values of the variables after running the M-file
CECOS College of Engineering and IT

March – July 2012
Comments
 While writing any code, it’s a good practice to write proper comments
against each instruction.
 In MATLAB, any thing written after “%” is treated as a comment and is
ignored by the compiler.

CECOS College of Engineering and IT

March – July 2012
Function M-files
 User defined functions
 These M-files start with a keyword, “function”
 The first line of these files should look like
function[output variables]=functionName(input variables)
 These files are saved with the funtionName.m as file name.

 An example:

CECOS College of Engineering and IT

March – July 2012
Function M-files
 Calling this function from command window

CECOS College of Engineering and IT

March – July 2012
Function M-files
 Any comments after the first line becomes the help for that function

 Command window

CECOS College of Engineering and IT

March – July 2012
Tasks
1- Generate a vector of 50 elements having random values between 0
and 50.
2- Generate a vector, containing all the odd numbers between 0 and
100.
3- Create a function M-file that accepts two numbers and generates

there sum, product and difference as output.
4- Use Cramer's rule to solve the following set of equations.
3x1 - 1x2 + 0x3 = 1
1x1 + 4x2 - 2x3 = 5
0x1 - 2x2 + 8x3 = 6
CECOS College of Engineering and IT

March – July 2012

More Related Content

PDF
matlab no1
PDF
PDF
PDF
PDF
PDF
DOC
Xi practical file
PPTX
source code metrics and other maintenance tools and techniques
matlab no1
Xi practical file
source code metrics and other maintenance tools and techniques

What's hot (11)

PPTX
Convolution using Scilab
PPTX
Linear regression model
PPT
Function overloading in c++
DOCX
Cis 355 i lab 3 of 6
PDF
Introduction to simulink (1)
PDF
Sienna 8 countingsorts
PDF
Tte 451 operations research fall 2021 part 1
PDF
ECET350 Week 3 Homework Assignment
PDF
ncaca2016
PDF
R programmingmilano
PPT
Binary operator overloading
Convolution using Scilab
Linear regression model
Function overloading in c++
Cis 355 i lab 3 of 6
Introduction to simulink (1)
Sienna 8 countingsorts
Tte 451 operations research fall 2021 part 1
ECET350 Week 3 Homework Assignment
ncaca2016
R programmingmilano
Binary operator overloading
Ad

Viewers also liked (10)

PDF
PDF
PDF
PPT
communication system Chapter 5
PPT
communication system Chapter 6
PPT
communication system ch1
PDF
PPT
communication system Chapter 3
PPT
communication system Chapter 4
PPT
communication system Chapter 2
communication system Chapter 5
communication system Chapter 6
communication system ch1
communication system Chapter 3
communication system Chapter 4
communication system Chapter 2
Ad

Similar to matab no2 (20)

PPTX
2022-S1-IT2070-Lecture-06-Algorithms.pptx
PDF
Introduction to MATLAB
PPTX
Chap#0_Introducation_to_nsnsnnMATLAB.pptx
PDF
Chapter 16-spreadsheet1 questions and answer
DOCX
MATLAB guide
PPTX
Introduction to MATLAB
PPT
Tony Von Gusmann & MS BI
PPT
Introduction to matlab
PDF
Mathematical Modeling using MATLAB, by U.M. Sundar Senior Application Enginee...
PPSX
matlab-130408153714-phpapp02_lab123.ppsx
PPTX
Matlab-1.pptx
PPT
473431331-Matlab-Simulink-Tutorial-ppt.ppt
PPSX
Matlab basic and image
PDF
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
PPT
Matlab.ppt
PPT
OverheadsDay1.ppt
PDF
Implementation of a new Size Estimation Model
PPSX
Business Intelligence Portfolio
PPTX
Matlab ppt
PPTX
python programming internship presentation.pptx
2022-S1-IT2070-Lecture-06-Algorithms.pptx
Introduction to MATLAB
Chap#0_Introducation_to_nsnsnnMATLAB.pptx
Chapter 16-spreadsheet1 questions and answer
MATLAB guide
Introduction to MATLAB
Tony Von Gusmann & MS BI
Introduction to matlab
Mathematical Modeling using MATLAB, by U.M. Sundar Senior Application Enginee...
matlab-130408153714-phpapp02_lab123.ppsx
Matlab-1.pptx
473431331-Matlab-Simulink-Tutorial-ppt.ppt
Matlab basic and image
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
Matlab.ppt
OverheadsDay1.ppt
Implementation of a new Size Estimation Model
Business Intelligence Portfolio
Matlab ppt
python programming internship presentation.pptx

Recently uploaded (20)

PDF
My India Quiz Book_20210205121199924.pdf
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
Hazard Identification & Risk Assessment .pdf
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
advance database management system book.pdf
PDF
1_English_Language_Set_2.pdf probationary
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
IGGE1 Understanding the Self1234567891011
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Computer Architecture Input Output Memory.pptx
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PPTX
Virtual and Augmented Reality in Current Scenario
My India Quiz Book_20210205121199924.pdf
History, Philosophy and sociology of education (1).pptx
Hazard Identification & Risk Assessment .pdf
What if we spent less time fighting change, and more time building what’s rig...
Share_Module_2_Power_conflict_and_negotiation.pptx
advance database management system book.pdf
1_English_Language_Set_2.pdf probationary
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Practical Manual AGRO-233 Principles and Practices of Natural Farming
TNA_Presentation-1-Final(SAVE)) (1).pptx
IGGE1 Understanding the Self1234567891011
AI-driven educational solutions for real-life interventions in the Philippine...
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Computer Architecture Input Output Memory.pptx
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Virtual and Augmented Reality in Current Scenario

matab no2

  • 1. Lab No.02 M-files and Matrix operations Designed by : Dawar Awan dawar@cecos.edu.pk CECOS College of Engineering and IT March – July 2012
  • 2. Matrices  MATLAB treats every thing as a matrix (check the Workspace)  1-by-1 matrices are interpreted as scalars  Matrices with only one row or one column are known as vectors  A matrix is a rectangular array of numbers. 1 9 2 6 7 8 3 5 1 8 2 4 A matrix with 3 rows, 4 columns, and 12 elements CECOS College of Engineering and IT March – July 2012
  • 3. Defining matrices >> A = [1 2 3; 4 5 6; 7 8 9]; >> A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; and >> A = [ 1 2 3 456 7 8 9 ]; Defines A= 1 3 4 5 6 7 CECOS College of Engineering and IT 2 8 9 March – July 2012
  • 4. Accessing matrix elements  The matrix element located in the i-th row and j-th column of “A” is referred to as, A(i,j)  Try the following instruction and observe the change in “A” >> A(2,3) = 10 CECOS College of Engineering and IT March – July 2012
  • 5. Some Built-in matrix functions Function Description diag eye magic ones rand triu tril zeros size length find : : : : : : : : : : : CECOS College of Engineering and IT returns diagonal elements as vector identity matrix magic square matrix of ones randomly generated matrix upper triangular part of a matrix lower triangular part of a matrix matrix of zeros Number of rows and columns in a matrix Length of an array find indices of some elements in array March – July 2012
  • 6. Matrix functions : Examples >> a=[1 2 3;4 5 6;7 8 9] a= 1 4 7 2 5 8 3 6 9 >> diag(a) >> triu(a) >> tril(a) >> size(a) >> length(a) ans = ans = ans = ans = ans = 1 5 9 CECOS College of Engineering and IT 1 0 0 2 5 0 3 6 9 1 4 7 0 5 8 0 0 9 3 3 3 March – July 2012
  • 7. Matrix functions : Examples >> eye(3) >> magic(3) >> rand(3) ans = ans = ans = 1 0 0 0 1 0 0 0 1 8 3 4 1 5 9 6 7 2 0.8147 0.9134 0.2785 0.9058 0.6324 0.5469 0.1270 0.0975 0.9575 >> ones(3) >> ones(3,2) >> zeros(3) >> zeros(3,2) ans = ans = ans = ans = 1 1 1 1 1 1 1 1 1 CECOS College of Engineering and IT 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 March – July 2012
  • 8. Matrix functions : Examples >> rand(3,2) ans = 0.9649 0.9572 0.1576 0.4854 0.9706 0.8003 CECOS College of Engineering and IT March – July 2012
  • 9. Matrix operations + * ^ ' / => => => => => => Addition Subtraction Multiplication Power Conjugate transpose Division  To perform index by index operation, use dot (.) notation ./ .* .^ .' => => => => CECOS College of Engineering and IT Index by index division Index by index multiplication Index by index power Non conjugate transpose March – July 2012
  • 10. Dot notation  Observe the change in result due to the “dot” CECOS College of Engineering and IT March – July 2012
  • 11. Examples/Exercise 1 4 7 C= B= A= 2 5 8 3 6 9 1 2 3 1 2 3 1 2 3 1 D= 2 1 2 2 1 2 1  Practice the following matrix operations A+B, A’, A*B, 2*A, A/2, A/B, A./B, C/D, C./D, C*D, C.*D, C^2, C.^2 CECOS College of Engineering and IT March – July 2012
  • 12. Colon notation CECOS College of Engineering and IT March – July 2012
  • 13. M - files  M-files are script/text files containing MATLAB commands Script M-file Function M-file  To make a script M-file, you need to open a file using the built-in MATLAB editor.  There are two ways to accomplish it: 1. From file menu, click NEW 2. Type edit on command line CECOS College of Engineering and IT March – July 2012
  • 14. M - files  To save the M-file go to the “file” menu and click on “save”.  The name should not contain spaces, should not start with a number and should not be same as any built-in function.  The extension of this file should be “.m” CECOS College of Engineering and IT March – July 2012
  • 15. M - files  This code can now be executed in two ways 1. Write the name of file on command window (excluding “.m”) 2. Under the “debug” menu, click “Run”  The variables used in the M-file will be maintained in the workspace and hence accessible from the command window. CECOS College of Engineering and IT March – July 2012
  • 16. M - files  Code written in M-file to calculate various parameters of a circle  Check the values of the variables after running the M-file CECOS College of Engineering and IT March – July 2012
  • 17. Comments  While writing any code, it’s a good practice to write proper comments against each instruction.  In MATLAB, any thing written after “%” is treated as a comment and is ignored by the compiler. CECOS College of Engineering and IT March – July 2012
  • 18. Function M-files  User defined functions  These M-files start with a keyword, “function”  The first line of these files should look like function[output variables]=functionName(input variables)  These files are saved with the funtionName.m as file name.  An example: CECOS College of Engineering and IT March – July 2012
  • 19. Function M-files  Calling this function from command window CECOS College of Engineering and IT March – July 2012
  • 20. Function M-files  Any comments after the first line becomes the help for that function  Command window CECOS College of Engineering and IT March – July 2012
  • 21. Tasks 1- Generate a vector of 50 elements having random values between 0 and 50. 2- Generate a vector, containing all the odd numbers between 0 and 100. 3- Create a function M-file that accepts two numbers and generates there sum, product and difference as output. 4- Use Cramer's rule to solve the following set of equations. 3x1 - 1x2 + 0x3 = 1 1x1 + 4x2 - 2x3 = 5 0x1 - 2x2 + 8x3 = 6 CECOS College of Engineering and IT March – July 2012