Professional Documents
Culture Documents
, 3 2015
: D A N I L O S T E PA N O V I C , I N T R O D U C T I O N T O P R O G R A M M I N G I N M AT L A B , , 2 0 1 0
MATLAB, the language of technical
computing
!
()
, ,
,
(user interface) .
MATHWORKS 1984
>1
1500 MATLAB 30 !
Matrix laboratory
MAT LAB
MATRIX LABORATOR
MATLAB .
.
MATLAB
1
, (scripts)
2
3
4
1-
MATLAB
Command Window-
Workspace-
Current Folder- ()
Editor- - (m-files)
! MATLAB
.. Day 1/data
Day1/data
Help/Doc
MATLAB help
>> help sum
.. diff prod
doc
>> doc sum
doc -
>> doc average
Scripts-
scripts
(editor) MATLAB
.m (m-files)
(command line)
>> edit hellochristina.m
New Script- scripSave As*.m
(Editor)
Scripts
% !
>> help
script .
script ,
(workspace).
script helloworld.m
:
Hello World! I will learn MATLAB today!
: () command line disp.m
(string), : this is a string
function helloworld
%a program which displays a happy sentence to the world!
disp('Hello World')
disp('I will learn MATLAB today!')
Run
(variables)
64-bit double (/default)- 264
Floating point- significant x baseexponent
(decimals) ..
1.2345=12345x10-4
double .
MATLAB uint8 ( used unsigned 8 bit integer)
28. .. MATLAB 0-255.
>> out_d=double(out)
>> out_8=uint8(out_d)
MATLAB
>>a 16-bit char
:
>>var1=2.54
>>name1=Andreas
-
, _ , .. out_53, meas345, Chris10_b
-
(case sensitive), data_1 Data_1
/Scalars
>> a=1.5
>> b=pi*2.3+a*2.1-5
>> c=a+b+8;
(arrays)
H MATLAB 2 :
( )
(cell-arrays- )
>> row=[1,2,1.3,5,-7]
>> row=[1 2 1.3 5 -7]
row =
num2str:
string
save/clear/load
save Workspace .mat
p.x.
>> a=10;
>> b=20;
>>save Variables a b
Variables.mat a b (
Workspace ). directory .
>> clear all /clear a b Workspace
>>load Variables Workspace
save Workspace
>>save Day1
>>2+3
>>(2+i)*(3-2i)
>>1/0
>> 2^3
>> (((2+3i)^2-6)*8)/5
>> 8(3+5)----!
clc command window
>> clc
(built-in)
H MATLAB !
MATLAB 6-
In ** seconds, I will know **% MATLAB
1.5 .
num2str .
>>secperday=24*60*60;
>>tau=secperday*1.5;
>>durofClass=6*60*60;
>>knowledgeatend=1-exp(-durofClass/tau);
>>disp(['at the end of 21600 seconds of class, I will know ' num2str(knowledgeatend*100) '% of
MATLAB'])
at the end of 21600 seconds of class, I will know 15.3518% of MATLAB
/Transpose
( )
.
Oi
>>row=[1 2 3];
>>column=[1;2;3];
>>row+column !!
>> row+column row+column
>> 5*row
,
(*/^)
:
.*, ./, .^
!
*
[nxm] * [mxk]
! [2x2], [3x3]
.
.
1 0.
(n) n-
(row, column) (index).
( max)
find
:
sub2ind ind2sub
2-
(plotting)
linspace()
min max
y x
x
Plot()
plot() .
!
2 !
!
plot()
plot() , ( marker)
:
>> plot(x,y,.)
b blue . point - solid
y yellow s square
k black d diamond
^ triangle (up)
p pentagram
h hexagram
Plot()
>>doc ColorSpec
,
EditFigure Properties
! log() MATLAB
(ln)
loglog
..
(3D plots)
plot3()
H plot() 3 2
xlim, ylim kai zlim ..
>>xlim([3 5]);ylim([1 2]);zlim([3 6])
>>axis square
( )
>>axis tight
>>axis equal
>>axis ij
( )
subplot()
>>subplot(2,3,1)
2 3 ()
.
(3 )
(CLOCLWISE)
>>subplot(2,3,4:6)
.fig open()
/
reshape
imagesc mat
/ (colormap)
>>colormap (gray)
>>colormap (cool)
surf
(VERTICES)
>>shading faceted
>>shading faceted
>>shading flat >>shading interp
sinc(x) (sin(x)/x) -2:2 1:4
4 ,
, . .
subplot(2,2,1);plot(x1,sin(x1)./x1,'ro')
subplot(2,2,2);plot(x1,sin(2*x1)./(2*x1),'k
-')
subplot(2,2,3);plot(x1,sin(3*x1)./(3*x1),'g
.-')
subplot(2,2,4);plot(x1,sin(4*x1)./(4*x1),'m
--')
3-
Functions- scripts
(INPUTS)
function [SENS,SPEC]=threshb(Im,GT,TR) FUNCTION DECLARATION
%Im is the input image in '.jpg' form
%GT is the ground truth matrix
%TR is the threshold
INPUTS
I=imread(Im); OUTPUTS
for i=1:size(I,1)
for j=1:size(I,2)
B(i,j,:)=(I(i,j,1)<=TR);
end
end
CP=classperf(B,GT);
SENS=CP.Sensitivity;
SPEC=CP.Specificity;
function ( .. x,y,z)
( ) !
Function overloading-
MATLAB
.
.. size()
varargin, varargout, nargin, nargout
function plot(x,y,varargin)
plot(x,y) % plain
plot(x,y,'LineWidth',4)
plot(x,y,'Marker','o','LineWidth',2)
plot(x,y,'Marker','o','LineWidth',2,'MarkerFaceColor','r')
(function declaration)
function plot(x,y,varargin)
sin(f1*x) f1 (input) 0:2,
16 .
function plotSin(f1,varargin);
if nargin==1
arg='.';
else arg=varargin{1};
end
x=linspace(0,2*pi,16*f1+1);
figure
plot(x,sin(x),arg);
scalars
If/else
(flow) MATLAB
>>if
.. >=0.8
abs(*)<1
>>end
-------------------------------------------------------------------------------------------
>>if
1
>>else
2
>>end
elseif./for
>>if 1
1
>>elseif 2
2
>>else
3
>>end
To for
(iterations)
-for loop
>> for i=1:10
if i^2<20
K(i)=i^2;
else
K(i)=i;
end
end
>> K
K=
1 4 9 16 5 6 7 8 9 10
To i loop (loop variable)
.
While
while for loop
(iterations)
>>while
>>end
!
loop!
while
, -
2n >=
function n=smallestint(a);
%computes the smallest non-negative integer for which 2^n>=a.
n = 0; %setting the initial value
while 2^n < a
n = n + 1;
end
n
find
find
-
for loops
:
tic toc
find
-Vectorization
(indexing)
loops.
,
.
.
1-20000 --- 10%
20000-40000 20%
>40000 30%
>3 , 10%
10-100
3 .
function tax=calctax(income,fz);
%calculates the payable tax in EUR based on income in EUR and familzy size
%(fz)
if income<20000
taxp=0.1;
else if income>20000&income<40000
taxp=0.2;
else taxp=0.3;
end
end
if fz>3
taxd=0.9;
else taxd=1;
end
tax=income*taxp*taxd;
taxes=ones(10,10);
>> for i=1:10
for j=1:10
taxes(i,j)=calctax(i*10000,j);
end
end
>> [X,Y]=meshgrid(10000:10000:100000,1:10);
>> surf(X,Y,taxes)
4-
- ;
, x=b
3 !
- /
- - 0
.
(Singular Value Decomposition)
MATLAB
P +1 -
MATLAB
polyfit()
=[-1 0 2] =[0 -1 3]
... -1:2
, x=0.5
() (odes)
mean, median, mode
bar chart
MATLAB
(uniform distribution)