An Anthropologist on Matlab. Data Analysis Functions (1) corrcoef(x) cov(x) cplxpair(x) cross(x,y)...

22
An Anthropologist on Matlab
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    220
  • download

    1

Transcript of An Anthropologist on Matlab. Data Analysis Functions (1) corrcoef(x) cov(x) cplxpair(x) cross(x,y)...

An Anthropologist on Matlab

Data Analysis Functions (1)

• corrcoef(x)

• cov(x)

• cplxpair(x)

• cross(x,y)

• cumprod(x)

• cumsum(x)

• del2(A)

• diff(x)

• dot(x,y)

• gradient(Z, dx, dy)

• Correlation coefficients

• Covariance matrix

• complex conjugate pairs

• vector cross product

• cumulative prod of cols

• cumulative sum of cols• five-point discrete Laplacian

• diff between elements

• vector dot product

• approximate gradient

Data Analysis Functions (2)

• histogram(x)

• max(x), max(x,y)

• mean(x)

• median(x)

• min(x), min(x,y)

• prod(x)

• sort(x)

• std(x)

• subspace(A,B)

• sum(x)

• Histogram or bar chart

• max component

• mean of cols

• median of cols

• minimum component

• product of elems in col

• sort cols (ascending)

• standard dev of cols

• angle between subspaces

• sum of elems per col

Symbolic Math Toolbox

Symbolic Expressions

• ‘1/(2*x^n)’

• cos(x^2) - sin(x^2)

• M = sym(‘[a , b ; c , d]’)

• f = int(‘x^3 / sqrt(1 - x)’, ‘a’, ‘b’)

Symbolic Expressions

diff(‘cos(x)’)

ans =

-sin(x)

det(M)

ans =

a*d - b * c

Symbolic Functions

• numden(m) - num & denom of polynomial

• symadd(f,g) - add symbolic polynomials

• symsub(f,g) - sub symbolic polynomials

• symmul(f,g) - mult symbolic polynomials

• symdiv(f,g) - div symbolic polynomials

• sympow(f,’3*x’) - raise f^3

Advanced Operations

• f = ‘1/(1+x^2)’

• g = ‘sin(x)’

compose(f,g) % f(g(x))

ans=

1/(1+sin(x)^2)

Advanced Operations

• finverse(x^2)

ans =

x^(1/2)

• symsum(‘(2*n - 1) ^ 2’, 1, ‘n’)

ans =

11/3*n + 8/3-4*(n+1)^2 + 4/3*(n+1)^3

Symbolic Differentiationf = ‘a*x^3 + x^2 + b*x - c’

diff(f) % by default wrt x

ans =

3*a^2 + 2*x + b

diff(f, ‘a’) % wrt a

ans =

x^3

diff(f,’a’,2) % double diff wrt a

ans =

0

Symbolic Integrationf = sin(s+2*x)

int(f)

ans =

-1/2*cos(s+2*x)

int(f,’s’)

ans =

-cos(s+2*x)

int(f, ‘s’, pi/2,pi)

ans=

-cos(s)

Comments &Punctuation (1)

• All text after a percentage sign (%) is ignored

>> % this is a comment

• Multiple commands can be placed on one line separated by commas (,)

>> A = magic(4), B = ones(4), C = eye(4)

Comments &Punctuation (2)

• A semicolon may be also used, either after a single command or multiple commands

>> A = magic(4); B = ones(4); C = eye(4);

• Ellipses (…) indicate a statement is continued on the next line

A = B/…

C

SAVE Command (1)

>> save• Store all the variables in binary format in a file

called matlab.mat

>> save fred• Store all the variables in binary format in a file

called fred.mat• >> save a b d fred• Store the variables a, b and d in fred.mat

SAVE Command (2)

>> save a b d fred -ascii• Stores the variables a, b and d in a file called

fred.mat in 8-bit ascii format

>> save a b d fred -ascii -double• Stores the variables a, b and d in a file called

fred.mat in 16-bit ascii format

Load Command

• Create a text file called mymatrix.dat with– 16.0 3.0 2.0 13.0– 5.0 10.0 11.0 8.0– 9.0 6.0 7.0 12.0– 4.0 15.0 14.0 1.0

• “load mymatrix.dat”, create variable mymatrix

M-Files

• To store your own MatLab commands in a file, create it as a text file and save it with a name that ends with “.m”

• So mymatrix.mA = […

16.0 3.0 2.0 13.0

5.0 10.0 11.0 8.0

9.0 6.0 7.0 12.0

4.0 15.0 14.0 1.0];

• type mymatrix

IF Condition

if condition {commands}end

If x = 2output = ‘x is even’end

WHILE Loop

while condition {commands}end

X = 10;count = 0;while x > 2x = x / 2;count = count + 1;end

FOR Loop

for x=array {commands}end

for n = 1:10x(n) = sin(n);end

A = zeros(5,5); % preallocfor n = 1:5for m = 5:-1:1A(n,m) = n^2 + m^2;enddisp(n)end

Creating a function

function a = gcd(a,b)

% GCD Greatest common divisor

% gcd(a,b) is the greatest common divisor of

% the integers a and b, not both zero.

a = round(abs(a)); b = round(abs(b));

if a == 0 & b == 0

error('The gcd is not defined when both numbers are zero')

else

while b ~= 0

r = rem(a,b);

a = b; b = r;

end

end

Quick Exercise (!)

• Consider Polynomial Addition again :

how would you write a program that takes in two polynomials and irrespective of their sizes it adds the polynomials together ? Given that the function length(A) returns the length of a vector.

Answers on a postcard to : [email protected]

oh, and while you’re here anyhow, if you have a browser open, please go to the following sites :

http://www.the hungersite.com

http://www.hitsagainsthunger.com