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

What's hot

Convolution using Scilab
Convolution using ScilabConvolution using Scilab
Convolution using Scilab
sachin achari
 
Linear regression model
Linear regression modelLinear regression model
Linear regression model
SatyamDeshPandey
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
BalajiGovindan5
 
Cis 355 i lab 3 of 6
Cis 355 i lab 3 of 6Cis 355 i lab 3 of 6
Cis 355 i lab 3 of 6
helpido9
 
Introduction to simulink (1)
Introduction to simulink (1)Introduction to simulink (1)
Introduction to simulink (1)
Memo Love
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsorts
chidabdu
 
Tte 451 operations research fall 2021 part 1
Tte 451  operations research   fall 2021   part 1Tte 451  operations research   fall 2021   part 1
Tte 451 operations research fall 2021 part 1
Wael ElDessouki
 
ECET350 Week 3 Homework Assignment
ECET350 Week 3 Homework AssignmentECET350 Week 3 Homework Assignment
ECET350 Week 3 Homework Assignment
BirleRubin
 
ncaca2016
ncaca2016ncaca2016
ncaca2016
Sushant Kumar
 
R programmingmilano
R programmingmilanoR programmingmilano
R programmingmilano
Ismail Seyrik
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
BalajiGovindan5
 

What's hot (11)

Convolution using Scilab
Convolution using ScilabConvolution using Scilab
Convolution using Scilab
 
Linear regression model
Linear regression modelLinear regression model
Linear regression model
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
 
Cis 355 i lab 3 of 6
Cis 355 i lab 3 of 6Cis 355 i lab 3 of 6
Cis 355 i lab 3 of 6
 
Introduction to simulink (1)
Introduction to simulink (1)Introduction to simulink (1)
Introduction to simulink (1)
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsorts
 
Tte 451 operations research fall 2021 part 1
Tte 451  operations research   fall 2021   part 1Tte 451  operations research   fall 2021   part 1
Tte 451 operations research fall 2021 part 1
 
ECET350 Week 3 Homework Assignment
ECET350 Week 3 Homework AssignmentECET350 Week 3 Homework Assignment
ECET350 Week 3 Homework Assignment
 
ncaca2016
ncaca2016ncaca2016
ncaca2016
 
R programmingmilano
R programmingmilanoR programmingmilano
R programmingmilano
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
 

Viewers also liked

matab no9
matab no9matab no9
Lab no.08
Lab no.08Lab no.08
matab no4
matab no4matab no4
communication system Chapter 5
communication system Chapter 5communication system Chapter 5
communication system Chapter 5
moeen khan afridi
 
communication system Chapter 6
communication system Chapter 6communication system Chapter 6
communication system Chapter 6
moeen khan afridi
 
communication system ch1
communication system ch1communication system ch1
communication system ch1
moeen khan afridi
 
Lab no.07
Lab no.07Lab no.07
communication system Chapter 3
communication system Chapter 3communication system Chapter 3
communication system Chapter 3
moeen khan afridi
 
communication system Chapter 4
communication system Chapter 4communication system Chapter 4
communication system Chapter 4
moeen khan afridi
 
communication system Chapter 2
communication system Chapter 2communication system Chapter 2
communication system Chapter 2
moeen khan afridi
 

Viewers also liked (10)

matab no9
matab no9matab no9
matab no9
 
Lab no.08
Lab no.08Lab no.08
Lab no.08
 
matab no4
matab no4matab no4
matab no4
 
communication system Chapter 5
communication system Chapter 5communication system Chapter 5
communication system Chapter 5
 
communication system Chapter 6
communication system Chapter 6communication system Chapter 6
communication system Chapter 6
 
communication system ch1
communication system ch1communication system ch1
communication system ch1
 
Lab no.07
Lab no.07Lab no.07
Lab no.07
 
communication system Chapter 3
communication system Chapter 3communication system Chapter 3
communication system Chapter 3
 
communication system Chapter 4
communication system Chapter 4communication system Chapter 4
communication system Chapter 4
 
communication system Chapter 2
communication system Chapter 2communication system Chapter 2
communication system Chapter 2
 

Similar to matab no2

2022-S1-IT2070-Lecture-06-Algorithms.pptx
2022-S1-IT2070-Lecture-06-Algorithms.pptx2022-S1-IT2070-Lecture-06-Algorithms.pptx
2022-S1-IT2070-Lecture-06-Algorithms.pptx
pradeepwalter
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
Dun Automation Academy
 
Chapter 16-spreadsheet1 questions and answer
Chapter 16-spreadsheet1  questions and answerChapter 16-spreadsheet1  questions and answer
Chapter 16-spreadsheet1 questions and answer
RaajTech
 
MATLAB guide
MATLAB guideMATLAB guide
MATLAB guide
Prerna Rathore
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
Ravikiran A
 
Tony Von Gusmann & MS BI
Tony Von Gusmann & MS BITony Von Gusmann & MS BI
Tony Von Gusmann & MS BI
vongusmann
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
Tarun Gehlot
 
matlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsxmatlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsx
lekhacce
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
aboma2hawi
 
Matlab basic and image
Matlab basic and imageMatlab basic and image
Matlab basic and image
Divyanshu Rasauria
 
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
chinthala Vijaya Kumar
 
Matlab.ppt
Matlab.pptMatlab.ppt
Matlab.ppt
KarimullaSk14
 
OverheadsDay1.ppt
OverheadsDay1.pptOverheadsDay1.ppt
OverheadsDay1.ppt
KarimullaSk14
 
Implementation of a new Size Estimation Model
Implementation of a new Size Estimation ModelImplementation of a new Size Estimation Model
Implementation of a new Size Estimation Model
ijsrd.com
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
dklawson
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
chestialtaff
 
Matlab-Assignment-Help-Turkey
Matlab-Assignment-Help-TurkeyMatlab-Assignment-Help-Turkey
Matlab-Assignment-Help-Turkey
Phdtopiccom
 
Day5
Day5Day5
Matlab anilkumar
Matlab  anilkumarMatlab  anilkumar
Matlab anilkumar
THEMASTERBLASTERSVID
 
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxMATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
prashantkumarchinama
 

Similar to matab no2 (20)

2022-S1-IT2070-Lecture-06-Algorithms.pptx
2022-S1-IT2070-Lecture-06-Algorithms.pptx2022-S1-IT2070-Lecture-06-Algorithms.pptx
2022-S1-IT2070-Lecture-06-Algorithms.pptx
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Chapter 16-spreadsheet1 questions and answer
Chapter 16-spreadsheet1  questions and answerChapter 16-spreadsheet1  questions and answer
Chapter 16-spreadsheet1 questions and answer
 
MATLAB guide
MATLAB guideMATLAB guide
MATLAB guide
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Tony Von Gusmann & MS BI
Tony Von Gusmann & MS BITony Von Gusmann & MS BI
Tony Von Gusmann & MS BI
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
matlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsxmatlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsx
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 
Matlab basic and image
Matlab basic and imageMatlab basic and image
Matlab basic and image
 
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
 
Matlab.ppt
Matlab.pptMatlab.ppt
Matlab.ppt
 
OverheadsDay1.ppt
OverheadsDay1.pptOverheadsDay1.ppt
OverheadsDay1.ppt
 
Implementation of a new Size Estimation Model
Implementation of a new Size Estimation ModelImplementation of a new Size Estimation Model
Implementation of a new Size Estimation Model
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
Matlab-Assignment-Help-Turkey
Matlab-Assignment-Help-TurkeyMatlab-Assignment-Help-Turkey
Matlab-Assignment-Help-Turkey
 
Day5
Day5Day5
Day5
 
Matlab anilkumar
Matlab  anilkumarMatlab  anilkumar
Matlab anilkumar
 
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxMATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
 

Recently uploaded

PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 

Recently uploaded (20)

PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 

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