SlideShare a Scribd company logo
MATLAB
Basics
MATLAB User Environment
Workspace/Variable
Inspector
Command History
Command Window
Getting help
There are several ways of getting help:
Basic help on named commands/functions is echoed to the command
window by:
>> help command-name
A complete help system containing full text of manuals is started by:
>> helpdesk
System Environment
• Windows
– MATLAB installed in c:matlab6.5
– Your code…anywhere convenient (e.g. h:matlab)
• Linux (Environment network)
– MATLAB installed in /apps/matlab
– Your code in /home/username/matlab
– Your environment configuration in ~/.matlab
startup.m
• The script $matlab_roottoolboxlocalmatlabrc.m
is always run at startup – it reads system
environment variables etc, and initialises
platform dependent settings. If present it will run
a user defined initialisation script: startup.m
– Linux: /home/user/matlab/startup.m
– Windows: $matlab_roottoolboxlocalstartup.m
• Use startup.m for setting paths, forcing
preferred settings, etc.
%-------------------------------------
% Matlab startup file for IMB's laptop
%-------------------------------------
%-- add paths for my m-files --
addpath d:/matlab
addpath d:/matlab/bulk2.5
addpath d:/matlab/bulk2.6
addpath d:/matlab/coastal
addpath d:/matlab/lidar
addpath d:/matlab/ndbc
addpath d:/matlab/page
addpath d:/matlab/sections
addpath d:/matlab/sharem
addpath d:/matlab/wavelet
addpath d:/matlab/LEM
addpath d:/matlab/GPSbook
addpath d:/matlab/FAAM
addpath d:/matlab/FAAM/winds
addpath d:/matlab/faam/bae
%-- add netCDF toolbox --
addpath c:/matlab701/bin
addpath c:/matlab701/bin/win32
addpath d:/matlab/netcdf
addpath d:/matlab/netcdf/ncfiles
addpath d:/matlab/netcdf/nctype
addpath d:/matlab/netcdf/ncutility
%-- add path for generic data --
addpath d:/matlab/coastlines % coastline data
addpath d:/cw96/flight_data/jun02 % raw cw96
addpath d:/cw96/flight_data/jun07 % aircraft data
addpath d:/cw96/flight_data/jun11
addpath d:/cw96/flight_data/jun12
addpath d:/cw96/flight_data/jun17
addpath d:/cw96/flight_data/jun19
addpath d:/cw96/flight_data/jun21
addpath d:/cw96/flight_data/jun23
addpath d:/cw96/flight_data/jun26
addpath d:/cw96/flight_data/jun29
addpath d:/cw96/flight_data/jul01
addpath d:/cw96/runs % run definitions for cw96 flights
%----------------------------------------------------------------------
%-- set default figure options --
set(0,'DefaultFigurePaperType','a4')
% this should be the default in EU anyway
set(0,'DefaultFigurePaperUnits','inches')
% v6 defaults to cm for EU countries
set(0,'DefaultFigureRenderer','painters')
% v7 default OpenGL causes problems
Example startup.m file (for my laptop)
• addpath – adds directories to
the search path. MATLAB will
look in ALL directories on the
path for:
– Functions and scripts (.m files)
– MATLAB data files (.mat files)
• It will also look in the current
directory
• The ‘set’ commands in the
example startup.m file set
some default graphics
properties, overriding the
defaults – will cover these
later.
The WORKSPACE
• MATLAB maintains an active workspace, any
variables (data) loaded or defined here are
always available.
• Some commands to examine workspace, move
around, etc:
>> who
Your variables are:
x y
who : lists variables in workspace
>> whos
Name Size Bytes Class
x 3x1 24 double array
y 3x2 48 double array
Grand total is 9 elements using 72 bytes
whos : lists names and basic properties of variables in the workspace
>> pwd
ans =
D:
>> cd cw96jun02
>> dir
. 30m_wtv.mat edson2km.mat jun02_30m_runs.mat
.. 960602_sst.mat edson_2km_bulk.mat
pwd, cd, dir, ls : similar to operating system (but no option switches)
VARIABLES
• Everything (almost) is treated as a double-
precision floating point array by default
– Typed variables (integer, float, char,…) are supported,
but usually used only for specific applications. Not all
operations are supported for all typed variables.
– [IDL uses typed variables, but allows mixing of
types...at least to some extent]
>> x=[1 2 3]
x =
1 2 3
>> x=[1,2,3]
x =
1 2 3
>> x=[1
2
3
4];
>> x=[1;2;3;4]
x =
1
2
3
4
When defining variables, a space or
comma separates elements on a
row.
A newline or semicolon forces a
new row; these 2 statements are
equivalent.
NB. you can break definitions across
multiple lines.
• 1 & 2D arrays are treated as formal matrices
– Matrix algebra works by default:
>> a=[1 2];
>> b=[3
4];
>> a*b
ans =
11
>> b*a
ans =
3 6
4 8
1x2 row oriented array (vector)
(Trailing semicolon suppresses display of output)
2x1 column oriented array
Result of matrix multiplication depends on
order of terms (non-cummutative)
• Element-by-element operation is forced by
preceding operator with ‘.’
>> a=[1 2];
>> b=[3
4];
>> a.*b
??? Error using ==> times
Matrix dimensions must agree. Size and shape must match
>> a=[1 2]
A =
1 2
>> b=[3 4];
>> a.*b
ans =
3 8
>> c=a+b
c =
4 6
Matrix addition & subtraction
operate element-by-element
anyway. Dimensions of
matrix must still match!
No trailing semicolon,
immediate display of result
Element-by-element
multiplication
>> A = [1:3;4:6;7:9]
A =
1 2 3
4 5 6
7 8 9
>> mean(A)
ans =
4 5 6
>> sum(A)
ans =
12 15 18
Most common functions operate on
columns by default
INDEXING ARRAYS
• MATLAB indexes arrays:
– 1 to N
– [row,column]
[1,1 1,2 . 1,n
2,1 2,2 . 2,n
3,1 3,2 . 3,n
. . .
m,1 m,2 . m,n]
• IDL indexes arrays:
– 0 to N-1
– [column,row]
[0,0 1,0 . n-1,0
0,1 1,1 . n-1,1
0,2 1,2 . n-1,2
. . . .
0,m-1 1,m-1 . n-1,m-1]
n
m
>> A = [1:3;4:6;7:9]
A =
1 2 3
4 5 6
7 8 9
>> A(2,3)
ans =
6
>> A(1:3,2)
ans =
2
5
8
>> A(2,:)
ans =
4 5 6
The colon indicates a range, a:b (a to b)
A colon on its own indicates ALL values
THE COLON OPERATOR
• Colon operator occurs in several forms
– To indicate a range (as above)
– To indicate a range with non-unit increment
>> N = 5:10:35
N =
5 15 25 35
>> P = [1:3; 30:-10:10]
P =
1 2 3
30 20 10
• To extract ALL the elements of an array (extracts
everything to a single column vector)
>> A = [1:3; 10:10:30;
100:100:300]
A =
1 2 3
10 20 30
100 200 300
>> A(:)
ans =
1
10
100
2
20
200
3
30
300
LOGICAL INDEXING
• Instead of indexing arrays directly, a logical
mask can be used – an array of same size, but
consisting of 1s and 0s – usually derived as
result of a logical expression.
>> X = [1:10]
X =
1 2 3 4 5 6 7 8 9 10
>> ii = X>6
ii =
0 0 0 0 0 0 1 1 1 1
>> X(ii)
ans =
7 8 9 10
Basic Operators
+, -, *, / : basic numeric operators
 : left division (matrix division)
^ : raise to power
’ : transpose (of matrix) – flip along diagonal
• fliplr(), flipud() : flip matrix about
vertical and horizontal axes.
SAVING DATA
• MATLAB uses its own platform independent file format
for saving data – files have a .mat extension
– The ‘save’ command saves variables from the
workspace to a named file (or matlab.mat if no
filename given)
• save filename – saves entire workspace to filename.mat
• save var1 var2 … filename – saves named variables
to filename.mat
– By default save overwrites an existing file of the same
name, the –append switch forces appending data to
an existing file (but variables of same name will be
overwritten!)
• save var1 var2 filename -append
– Data is recovered with the ‘load’ command
• load filename – loads entire .mat file
• load filename var1 var2 …– loads named variables
• load filename –ascii – loads contents of an ascii
flatfile in a variable ‘filename’.
The ascii file must contain a rectangular array of numbers so
that it loads into a single matrix.
• X=load(‘filename’,’-ascii’) – loads the ascii file into
a variable ‘X’
• save var1 filename –ascii – saves a single variable
to an ascii flat file (rectangular array of numbers)
• There have been changes to the internal format of .mat files
between MATLAB v4 and v5 (major changes to allow arrays
with more than 2 dimensions, structures, cell arrays…), and
again with v7 (minor change to use unicode instead of ascii).
Later versions will load old format files. You can force save to
an old file format with –v4 and –v6 switches
save filename –v6

More Related Content

What's hot

Data transformation-cheatsheet
Data transformation-cheatsheetData transformation-cheatsheet
Data transformation-cheatsheet
Dieudonne Nahigombeye
 
Aaa ped-5-Data manipulation: Pandas
Aaa ped-5-Data manipulation: Pandas Aaa ped-5-Data manipulation: Pandas
Aaa ped-5-Data manipulation: Pandas
AminaRepo
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical Computing
Naveed Rehman
 
R Basics
R BasicsR Basics
Data import-cheatsheet
Data import-cheatsheetData import-cheatsheet
Data import-cheatsheet
Dieudonne Nahigombeye
 
ADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASADADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
Prof. Dr. K. Adisesha
 
R training3
R training3R training3
R training3
Hellen Gakuruh
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat Sheet
ACASH1011
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
Alberto Labarga
 
Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Stata Cheat Sheets (all)
Stata Cheat Sheets (all)
Laura Hughes
 
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي   R program د.هديل القفيديمحاضرة برنامج التحليل الكمي   R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
مركز البحوث الأقسام العلمية
 
Pandas csv
Pandas csvPandas csv
Pandas csv
Devashish Kumar
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
GauravPatil318
 
R Get Started I
R Get Started IR Get Started I
R Get Started I
Sankhya_Analytics
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scalaMeetu Maltiar
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 

What's hot (20)

Data transformation-cheatsheet
Data transformation-cheatsheetData transformation-cheatsheet
Data transformation-cheatsheet
 
Aaa ped-5-Data manipulation: Pandas
Aaa ped-5-Data manipulation: Pandas Aaa ped-5-Data manipulation: Pandas
Aaa ped-5-Data manipulation: Pandas
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical Computing
 
R Basics
R BasicsR Basics
R Basics
 
Data import-cheatsheet
Data import-cheatsheetData import-cheatsheet
Data import-cheatsheet
 
Scala collections
Scala collectionsScala collections
Scala collections
 
ADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASADADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASAD
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
 
R training3
R training3R training3
R training3
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat Sheet
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 
Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Stata Cheat Sheets (all)
Stata Cheat Sheets (all)
 
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي   R program د.هديل القفيديمحاضرة برنامج التحليل الكمي   R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
 
Pandas csv
Pandas csvPandas csv
Pandas csv
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
LectureNotes-05-DSA
LectureNotes-05-DSALectureNotes-05-DSA
LectureNotes-05-DSA
 
R Get Started I
R Get Started IR Get Started I
R Get Started I
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scala
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
Arrays
ArraysArrays
Arrays
 

Similar to Matlab basics

MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
AkashSingh728626
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
chestialtaff
 
introduction to matlab.pptx
introduction to matlab.pptxintroduction to matlab.pptx
introduction to matlab.pptx
Dr. Thippeswamy S.
 
Matlab Manual
Matlab ManualMatlab Manual
Lecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdfLecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdf
ssuserff72e4
 
Dsp file
Dsp fileDsp file
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
ssuser772830
 
matlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsxmatlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsx
lekhacce
 
Lecture 01 variables scripts and operations
Lecture 01   variables scripts and operationsLecture 01   variables scripts and operations
Lecture 01 variables scripts and operations
Smee Kaem Chann
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
reddyprasad reddyvari
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
joellivz
 
Matlab pt1
Matlab pt1Matlab pt1
Matlab pt1
Austin Baird
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
aboma2hawi
 
Matlab Tutorial.ppt
Matlab Tutorial.pptMatlab Tutorial.ppt
Matlab Tutorial.ppt
RaviMuthamala1
 

Similar to Matlab basics (20)

MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
introduction to matlab.pptx
introduction to matlab.pptxintroduction to matlab.pptx
introduction to matlab.pptx
 
Matlab Manual
Matlab ManualMatlab Manual
Matlab Manual
 
Lecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdfLecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdf
 
Dsp file
Dsp fileDsp file
Dsp file
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
matlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsxmatlab-130408153714-phpapp02_lab123.ppsx
matlab-130408153714-phpapp02_lab123.ppsx
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
 
Lecture 01 variables scripts and operations
Lecture 01   variables scripts and operationsLecture 01   variables scripts and operations
Lecture 01 variables scripts and operations
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
 
MATLAB & Image Processing
MATLAB & Image ProcessingMATLAB & Image Processing
MATLAB & Image Processing
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
 
Matlab pt1
Matlab pt1Matlab pt1
Matlab pt1
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 
Matlab Tutorial.ppt
Matlab Tutorial.pptMatlab Tutorial.ppt
Matlab Tutorial.ppt
 

Recently uploaded

Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptxTOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
nikitacareer3
 
Self-Control of Emotions by Slidesgo.pptx
Self-Control of Emotions by Slidesgo.pptxSelf-Control of Emotions by Slidesgo.pptx
Self-Control of Emotions by Slidesgo.pptx
iemerc2024
 
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
dxobcob
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
Kamal Acharya
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
yokeleetan1
 
AIR POLLUTION lecture EnE203 updated.pdf
AIR POLLUTION lecture EnE203 updated.pdfAIR POLLUTION lecture EnE203 updated.pdf
AIR POLLUTION lecture EnE203 updated.pdf
RicletoEspinosa1
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
manasideore6
 

Recently uploaded (20)

Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptxTOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
TOP 10 B TECH COLLEGES IN JAIPUR 2024.pptx
 
Self-Control of Emotions by Slidesgo.pptx
Self-Control of Emotions by Slidesgo.pptxSelf-Control of Emotions by Slidesgo.pptx
Self-Control of Emotions by Slidesgo.pptx
 
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
 
AIR POLLUTION lecture EnE203 updated.pdf
AIR POLLUTION lecture EnE203 updated.pdfAIR POLLUTION lecture EnE203 updated.pdf
AIR POLLUTION lecture EnE203 updated.pdf
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
 

Matlab basics

  • 3. Getting help There are several ways of getting help: Basic help on named commands/functions is echoed to the command window by: >> help command-name A complete help system containing full text of manuals is started by: >> helpdesk
  • 4. System Environment • Windows – MATLAB installed in c:matlab6.5 – Your code…anywhere convenient (e.g. h:matlab) • Linux (Environment network) – MATLAB installed in /apps/matlab – Your code in /home/username/matlab – Your environment configuration in ~/.matlab
  • 5. startup.m • The script $matlab_roottoolboxlocalmatlabrc.m is always run at startup – it reads system environment variables etc, and initialises platform dependent settings. If present it will run a user defined initialisation script: startup.m – Linux: /home/user/matlab/startup.m – Windows: $matlab_roottoolboxlocalstartup.m • Use startup.m for setting paths, forcing preferred settings, etc.
  • 6. %------------------------------------- % Matlab startup file for IMB's laptop %------------------------------------- %-- add paths for my m-files -- addpath d:/matlab addpath d:/matlab/bulk2.5 addpath d:/matlab/bulk2.6 addpath d:/matlab/coastal addpath d:/matlab/lidar addpath d:/matlab/ndbc addpath d:/matlab/page addpath d:/matlab/sections addpath d:/matlab/sharem addpath d:/matlab/wavelet addpath d:/matlab/LEM addpath d:/matlab/GPSbook addpath d:/matlab/FAAM addpath d:/matlab/FAAM/winds addpath d:/matlab/faam/bae %-- add netCDF toolbox -- addpath c:/matlab701/bin addpath c:/matlab701/bin/win32 addpath d:/matlab/netcdf addpath d:/matlab/netcdf/ncfiles addpath d:/matlab/netcdf/nctype addpath d:/matlab/netcdf/ncutility %-- add path for generic data -- addpath d:/matlab/coastlines % coastline data addpath d:/cw96/flight_data/jun02 % raw cw96 addpath d:/cw96/flight_data/jun07 % aircraft data addpath d:/cw96/flight_data/jun11 addpath d:/cw96/flight_data/jun12 addpath d:/cw96/flight_data/jun17 addpath d:/cw96/flight_data/jun19 addpath d:/cw96/flight_data/jun21 addpath d:/cw96/flight_data/jun23 addpath d:/cw96/flight_data/jun26 addpath d:/cw96/flight_data/jun29 addpath d:/cw96/flight_data/jul01 addpath d:/cw96/runs % run definitions for cw96 flights %---------------------------------------------------------------------- %-- set default figure options -- set(0,'DefaultFigurePaperType','a4') % this should be the default in EU anyway set(0,'DefaultFigurePaperUnits','inches') % v6 defaults to cm for EU countries set(0,'DefaultFigureRenderer','painters') % v7 default OpenGL causes problems Example startup.m file (for my laptop)
  • 7. • addpath – adds directories to the search path. MATLAB will look in ALL directories on the path for: – Functions and scripts (.m files) – MATLAB data files (.mat files) • It will also look in the current directory • The ‘set’ commands in the example startup.m file set some default graphics properties, overriding the defaults – will cover these later.
  • 8. The WORKSPACE • MATLAB maintains an active workspace, any variables (data) loaded or defined here are always available. • Some commands to examine workspace, move around, etc: >> who Your variables are: x y who : lists variables in workspace
  • 9. >> whos Name Size Bytes Class x 3x1 24 double array y 3x2 48 double array Grand total is 9 elements using 72 bytes whos : lists names and basic properties of variables in the workspace >> pwd ans = D: >> cd cw96jun02 >> dir . 30m_wtv.mat edson2km.mat jun02_30m_runs.mat .. 960602_sst.mat edson_2km_bulk.mat pwd, cd, dir, ls : similar to operating system (but no option switches)
  • 10. VARIABLES • Everything (almost) is treated as a double- precision floating point array by default – Typed variables (integer, float, char,…) are supported, but usually used only for specific applications. Not all operations are supported for all typed variables. – [IDL uses typed variables, but allows mixing of types...at least to some extent]
  • 11. >> x=[1 2 3] x = 1 2 3 >> x=[1,2,3] x = 1 2 3 >> x=[1 2 3 4]; >> x=[1;2;3;4] x = 1 2 3 4 When defining variables, a space or comma separates elements on a row. A newline or semicolon forces a new row; these 2 statements are equivalent. NB. you can break definitions across multiple lines.
  • 12. • 1 & 2D arrays are treated as formal matrices – Matrix algebra works by default: >> a=[1 2]; >> b=[3 4]; >> a*b ans = 11 >> b*a ans = 3 6 4 8 1x2 row oriented array (vector) (Trailing semicolon suppresses display of output) 2x1 column oriented array Result of matrix multiplication depends on order of terms (non-cummutative)
  • 13. • Element-by-element operation is forced by preceding operator with ‘.’ >> a=[1 2]; >> b=[3 4]; >> a.*b ??? Error using ==> times Matrix dimensions must agree. Size and shape must match
  • 14. >> a=[1 2] A = 1 2 >> b=[3 4]; >> a.*b ans = 3 8 >> c=a+b c = 4 6 Matrix addition & subtraction operate element-by-element anyway. Dimensions of matrix must still match! No trailing semicolon, immediate display of result Element-by-element multiplication
  • 15. >> A = [1:3;4:6;7:9] A = 1 2 3 4 5 6 7 8 9 >> mean(A) ans = 4 5 6 >> sum(A) ans = 12 15 18 Most common functions operate on columns by default
  • 16. INDEXING ARRAYS • MATLAB indexes arrays: – 1 to N – [row,column] [1,1 1,2 . 1,n 2,1 2,2 . 2,n 3,1 3,2 . 3,n . . . m,1 m,2 . m,n] • IDL indexes arrays: – 0 to N-1 – [column,row] [0,0 1,0 . n-1,0 0,1 1,1 . n-1,1 0,2 1,2 . n-1,2 . . . . 0,m-1 1,m-1 . n-1,m-1] n m
  • 17. >> A = [1:3;4:6;7:9] A = 1 2 3 4 5 6 7 8 9 >> A(2,3) ans = 6 >> A(1:3,2) ans = 2 5 8 >> A(2,:) ans = 4 5 6 The colon indicates a range, a:b (a to b) A colon on its own indicates ALL values
  • 18. THE COLON OPERATOR • Colon operator occurs in several forms – To indicate a range (as above) – To indicate a range with non-unit increment >> N = 5:10:35 N = 5 15 25 35 >> P = [1:3; 30:-10:10] P = 1 2 3 30 20 10
  • 19. • To extract ALL the elements of an array (extracts everything to a single column vector) >> A = [1:3; 10:10:30; 100:100:300] A = 1 2 3 10 20 30 100 200 300 >> A(:) ans = 1 10 100 2 20 200 3 30 300
  • 20. LOGICAL INDEXING • Instead of indexing arrays directly, a logical mask can be used – an array of same size, but consisting of 1s and 0s – usually derived as result of a logical expression. >> X = [1:10] X = 1 2 3 4 5 6 7 8 9 10 >> ii = X>6 ii = 0 0 0 0 0 0 1 1 1 1 >> X(ii) ans = 7 8 9 10
  • 21. Basic Operators +, -, *, / : basic numeric operators : left division (matrix division) ^ : raise to power ’ : transpose (of matrix) – flip along diagonal • fliplr(), flipud() : flip matrix about vertical and horizontal axes.
  • 22. SAVING DATA • MATLAB uses its own platform independent file format for saving data – files have a .mat extension – The ‘save’ command saves variables from the workspace to a named file (or matlab.mat if no filename given) • save filename – saves entire workspace to filename.mat • save var1 var2 … filename – saves named variables to filename.mat – By default save overwrites an existing file of the same name, the –append switch forces appending data to an existing file (but variables of same name will be overwritten!) • save var1 var2 filename -append
  • 23. – Data is recovered with the ‘load’ command • load filename – loads entire .mat file • load filename var1 var2 …– loads named variables • load filename –ascii – loads contents of an ascii flatfile in a variable ‘filename’. The ascii file must contain a rectangular array of numbers so that it loads into a single matrix. • X=load(‘filename’,’-ascii’) – loads the ascii file into a variable ‘X’ • save var1 filename –ascii – saves a single variable to an ascii flat file (rectangular array of numbers)
  • 24. • There have been changes to the internal format of .mat files between MATLAB v4 and v5 (major changes to allow arrays with more than 2 dimensions, structures, cell arrays…), and again with v7 (minor change to use unicode instead of ascii). Later versions will load old format files. You can force save to an old file format with –v4 and –v6 switches save filename –v6