Matlab Talk
-
Upload
hrishikeshanand -
Category
Documents
-
view
215 -
download
0
Transcript of Matlab Talk
-
8/13/2019 Matlab Talk
1/43
MATLABProgramming I
-
8/13/2019 Matlab Talk
2/43
Outline
Examples What is Matlab? History Characteristics Organization of Matlab Toolboxes
Basic Applications Programming
-
8/13/2019 Matlab Talk
3/43
Example I
Obtain the result of matrix multiply
Matlab Code>>A=[4,6; 9,15; 8,7];>>B=[3,24,5; 7,19,2];>>C=A*B
C =54 210 32
132 501 7573 325 54
4 63 24 5
9 157 19 2
8 7
-
8/13/2019 Matlab Talk
4/43
C Code
#include #include #include #include #include #include
using namespace std;typedef vector Mat;void input(istream& in, Mat& a);Mat matrix_product(const Mat& a, const Mat& b);
void print(const Mat& a);
-
8/13/2019 Matlab Talk
5/43
C Codes
int main(int argc, char *argv[]){ifstream in1 ( "Matrix A.txt" );ifstream in2 ( "Matrix B.txt" );int row, row1, col, col1;in1>>row>>col;in2>>row1>>col1;
Mat a(row, vector(col));input(in1, a);Mat b(row1, vector(col1));input(in2, b);
print(matrix_product(a,b));system("PAUSE");return EXIT_SUCCESS;
}
-
8/13/2019 Matlab Talk
6/43
C Code Cond.void input(istream& in, Mat& a)
{for(int i = 0; i < a.size(); ++i)
for(int j = 0; j < a[0].size(); ++j)in>>a[i][j];
}
void print(const Mat& a){
for(int i = 0; i < a.size(); ++i){ cout
-
8/13/2019 Matlab Talk
7/43
C Code Cond.
Mat matrix_product(const Mat& a, const Mat& b){Mat c(a.size(), vector(b[0].size()));for(int i = 0; i < c.size(); ++i)for(int j = 0; j < c[0].size(); ++j){
c[i][j] = 0;for(int k = 0; k < b.size() ; ++k)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}return c;
}
-
8/13/2019 Matlab Talk
8/43
Example II
Calculate the transpose of the matrix:
Matlab Code>>A=[3,17,2;5,19,7;6,4,54]>>A^(-1)
ans =-0.9327 0.8505 -0.0757
0.2131 -0.1402 0.0103
0.0879 -0.0841 0.0262
3 17 2
5 19 7
6 4 54
-
8/13/2019 Matlab Talk
9/43
What is MATLAB?
Abbreviation of MATrix LABoratory
Is a high-performance language for
technical computing
It integrates computation, visualization,
and programming in an easy-to-useenvironment where problems andsolutions are expressed in familiarmathematical notation.
-
8/13/2019 Matlab Talk
10/43
-
8/13/2019 Matlab Talk
11/43
History
In 1970s Dr.Cleve MolerBased on Fortran on EISPACK and LINPACKOnly solve basic matrix calculation and graphic
1984 MathWorks Company by Cleve Moler andJack LittleMatlab 1.0 based on CIncluded image manipulations, multimedia etc.toolboxes
In 1990 run on WindowsSimulink, Notebook, Symbol Operation, etc. 21 Century
-
8/13/2019 Matlab Talk
12/43
Main Features
Powerful tool for matrix calculationsBasic data type is double complex matrix with subscript is 1High efficient/reliable algorithms
Multi-paradigm- interactive environment (no compiler)- programming
Most input/output parameters in Matlab
functions are variable User friendly with extensively help system
-
8/13/2019 Matlab Talk
13/43
MATLAB SYSTEM
Develop Tools
Toolboxes
Data-Accessing Tools
Stateflow
Generating-Codes Tools
Module Libraries
Third parties modules
Applications
DATA
C
Simulink
MATLAB
-
8/13/2019 Matlab Talk
14/43
Buildup of MATLAB
Development Environment MATLAB desktop and Command Window, a command history, an editorand debugger, and browsers for viewing help, the workspace, files, and thesearch path .
The MATLAB Mathematical Function Library A vast collection of computational algorithms ranging from elementary
functions to more sophisticated functions The MATLAB Language
A high-level matrix/array language with control flow statements,functions, data structures, input/output, and object-orientedprogramming features
GraphicsDisplay vectors and matrices as graphstwo-dimensional and three-dimensional data visualizationimage processinganimation and presentation graphics
The MATLAB Application Program Interface (API) Interchange the C/Fortran codes with Matlab codes
-
8/13/2019 Matlab Talk
15/43
Basic Applications I
Matrices and ArraysGenerating Arrays/MatricesElementary Matrices:
sum, transpose, diagonal, subscriptsVariables: does not require any typedeclarations or dimension statements
Operators: +,-,*,/, ^, etc.Examples of Expressions
-
8/13/2019 Matlab Talk
16/43
Generating Matrices
Directly enter A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
Use Functions
Example: B = zeros(1,5) row vector/arrayC = ones(7,1) column vector/arrayD = rand(5,8,3) multiple dimension matrix
Load/Import from files
zeros All 0 s ones All 1 s rand Uniformly distributed random elementsrandn Normally distributed random elements
-
8/13/2019 Matlab Talk
17/43
Elementary Matrices>>D=rand(3,2,3)D(:,:,1) = D(:,:,2) = D(:,:,3) =
0.9501 0.4860 0.4565 0.4447 0.9218 0.40570.2311 0.8913 0.0185 0.6154 0.7382 0.93550.6068 0.7621 0.8214 0.7919 0.1763 0.9169
>> sum(D(:,1,1))ans =
1.7881>> E=D(:,:,2)'E =
0.4565 0.0185 0.82140.4447 0.6154 0.7919
>> a=diag(D(:,:,3))a =
0.92180.9355
>> F=[D(1,:,1),D(2,:,3); D(1,:,1)+1, D(2,:,3)-1]F =
0.9501 0.4860 0.7382 0.93551.9501 1.4860 -0.2618 -0.0645
>>F(:,2)=[]F =
0.9501 0.7382 0.93551.9501 -0.2618 -0.0645
>> F(2:1:4)=[]F =
0.9501 0.9355 -0.0645
-
8/13/2019 Matlab Talk
18/43
Examples of Expression
>>rho = (1+sqrt(5))/2rho =
1.6180
>>a = abs(3+4i)a =
5
>>huge = exp(log(realmax))huge =
1.7977e+308 =1.8X10308
-
8/13/2019 Matlab Talk
19/43
Basic Application II
Graphic
-
8/13/2019 Matlab Talk
20/43
-
8/13/2019 Matlab Talk
21/43
Flow Control Examples I
Selector:2-way:if rand < 0.3 %Condition
flag = 0; %then_expression_1else
flag = 1; %then_expression_2end %endifMultiple-ways:
if A > B | switch (grade)'greater | case 0
elseif A < B | Excellent
'less | case 1 elseif A == B | Good 'equal | case 2
else | Average error('Unexpected situation') | otherwise
end | Failed
| end
-
8/13/2019 Matlab Talk
22/43
Flow Control Examples II
Loop>>Generate a matrixfor i = 1:2
for j = 1:6
H(i,j) = 1/(i+j);end
end>>HH =
0.5000 0.3333 0.2500 0.2000 0.1667 0.14290.3333 0.2500 0.2000 0.1667 0.1429 0.1250
-
8/13/2019 Matlab Talk
23/43
-
8/13/2019 Matlab Talk
24/43
Scripts
MATLAB simply executes the commands foundin the file Example% Investigate the rank of magic squares
r = zeros(1,32); %generation a row array 1X 32% ; tell the system does not display% the result on the screen
for n = 3:32r(n) = rank(magic(n));
endrbar(r) %display r in bar figure
-
8/13/2019 Matlab Talk
25/43
-
8/13/2019 Matlab Talk
26/43
Functions
Functions are M-files that can accept inputarguments and return output arguments.The names of the M-file and of the
function should be the same.
-
8/13/2019 Matlab Talk
27/43
Function Example I
Background of AIC AIC is used to compare the quality of nestedmodels
AIC requires a bias-adjustment in small samplesizes
1
2 ln( ) 2
2 ( 1)2 ln( ) 2 1
min( )
exp( 0.5 )
exp( 0.5 )
i i
ii R
r
r
AIC likelihood K
K K
AICad likelihood K n K
delta AIC AIC
deltaw
delta
-
8/13/2019 Matlab Talk
28/43
Function Head (Comments)
%AICad Function%Date:Dec.29th, 2008%Purpose: Calculate the adjusted% Akaike s Information Criteria (AIC)
%InPut: AIC% n (the sample size)% K (the number of parameters in the model)%OutPut: AICad% delta% weight%Functions Called: None%Called by: None
-
8/13/2019 Matlab Talk
29/43
IN Command Window
>>AIC=[-123; -241.7; -92.4; -256.9];>>n=12;>>K=[4;8;2;6];>>[AICad, delta, w] = CalAIC(AIC, n, K)
AICad = delta = w =
-83.0000 89.9000 0.0000-97.7000 75.2000 0.0000-80.4000 92.5000 0.0000
-172.9000 0 1.0000
-
8/13/2019 Matlab Talk
30/43
M file
function [AICad,delta,w]=CalAIC(AIC,n,K) AICad = AIC+2.*K.*(K+1);a = min(AICad);delta = AICad - a;b = exp(-0.5.*delta);c = sum(b);w = b./c;
-
8/13/2019 Matlab Talk
31/43
-
8/13/2019 Matlab Talk
32/43
Background of PCACond.
Principal Components Analysis (PCA) It finds weighted sums of samples or
observations (linear combinations) that arehighly correlated with the values of the response variables (e.g., species abundances)
PCA objectively chooses these weights in such a way
as to maximize the variance in the weighted sums
Based on correlations between response variables
.
-
8/13/2019 Matlab Talk
33/43
-
8/13/2019 Matlab Talk
34/43
IN Command Window
>>a=[66;48;26;15;11];>>b=[25;30;55;20;50];>>c=[24;31;42;49;58];>>[score1, score2]=PCA(a,b,c)
score1 = score2 =-2.2233 -0.1963-1.1519 -0.07430.8734 -1.08880.4871 1.51202.0147 -0.1526
-
8/13/2019 Matlab Talk
35/43
M filefunction [x11,x12,x13,x21,x22,x23]=PCA(a,b,c)
%Calculate the mean and population standard deviation of 3 speciesfor i=1:5
d(i)=(a(i)-mean(a))^2;stda=sqrt(sum(d)/5);e(i)=(b(i)-mean(b))^2;
stdb=sqrt(sum(e)/5);f(i)=(c(i)-mean(c))^2;stdc=sqrt(sum(f)/5);
end%Obtain the z-scores of 3 species
for i=1:5za(i)=(a(i)-mean(a))/stda;zb(i)=(b(i)-mean(b))/stdb;zc(i)=(c(i)-mean(c))/stdc;
endN=[za',zb',zc'];
-
8/13/2019 Matlab Talk
36/43
-
8/13/2019 Matlab Talk
37/43
M file Cond.
new=[a,b,c,score1];M2=[1,corr(a,b),corr(a,c),corr(a,score1);
corr(a,b),1,corr(b,c),corr(b,score1); corr(a,c), corr(b, c), 1, corr(c,score1); corr(a,score1),corr(b,score1),corr(c,score1),1];
for i=1:3for j=1:3
corrnew(i,j)=M2(i,j);end
endg=[M2(4,1)*M2(4,1),M2(4,1)*M2(4,2),M2(4,1)*M2(4,3);
M2(4,2)*M2(4,1),M2(4,2)*M2(4,2),M2(4,2)*M2(4,3); M2(4,3)*M2(4,1),M2(4,3)*M2(4,2),M2(4,3)*M2(4,3)];
corrmatrix=corrnew-g;[V2,D2]=eig(corrmatrix);weight2=V2(:,3); %signs needs to be checked with JMP
score2=N*weight2;
-
8/13/2019 Matlab Talk
38/43
MATLAB in MCSR
Matlab is available on willow and sweetgum www.mcsr.olemiss.edu [email protected]
http://www.mcsr.olemiss.edu/mailto:[email protected]:[email protected]://www.mcsr.olemiss.edu/ -
8/13/2019 Matlab Talk
39/43
-
8/13/2019 Matlab Talk
40/43
-
8/13/2019 Matlab Talk
41/43
Enter matlab can activate Matlab in willowor sweetgum
Use ver to obtain the version of Matlab,
which can tell you which toolboxes ofMatlab in that machine
Price of personal purchase is 500$ include updatereleases (only include MATLAB & Simulink)You need pay separate fee for each toolbox such asstatistical, bioinformatics, etc
-
8/13/2019 Matlab Talk
42/43
Take Home Message
-
8/13/2019 Matlab Talk
43/43