Vous êtes sur la page 1sur 20

MATLAB What is it?

Computing environment / programming


language
Tool for manipulating matrices

Many applications, you just need to get some


numbers in a matrix
Linear algebra, signal processing, image
processing, statistics, fMRI, EEG, modeling, neural
nets...

MATLAB is dumb, you are smart

Good info online

MathWorks- videos, links to university run


tutorials/resources, examples:

Union College and Southern Illinois tutorials are


very good

http://www.mathworks.com/academia/student_ce
nter/tutorials/launchpad.html

Naming variables

Can assign variable names to scalars, strings,


matrices, function outputs, etc...

>> x =
5

>> st =
'string'

x=

st =
5

>> stArray = {'steph' 'felix' 'don' 'mike'}'


stArray =
'steph'
'felix'
'don'
'mike'

string

>> xsq = x^2, xdiv = x/20


xsq =
25
xdiv =
0.2500

>> M = floor(randn(3,3))

>> dimM = size(M)

M=

dimM =

-1
-2
-2

-1
0
-1

1
0
-2

Vectors A special kind of matrix

Use square brackets to define, parentheses to


index (MATLAB indexes from 1, not 0)
Put spaces between numbers to create row
vector, semi-colons to create column vector
Apostrophe for transpose

>> v = [2 4 6 8]
v=

v2 =
2

>> v2 = [2; 4; 6;
8]

2
4
6
8

>>
v(3)

>> v2'
ans =

ans =
2
6

Vectors

Add, replace, delete elements using indexing

>> v = [2; 4; 6;
8]
v=
2
4
6
8

>> v(7) =
10
v=
2
4
6
8
0
0
10

>> v(2) =
15
v=
2
15
6
8
0
0
10

>> v(4) = [ ]
v=
2
15
6
0
0
10

Vectors

When defining, use colon to define set of


numbers with common increment, default
increment is 1
Can define increment

>> z = [1:5]

>> y = [1:2:10]

z=

y=
1

>> z2 = [3:8]

>> y2 = [4:.5:6]

z2 =

y2 =

4.0000

4.5000

5.0000

5.5000

6.0000

Vectors

Access subsets of elements using colon, or a


>> z2 = [3:8]
vector of indices
z2 =
8

>>
z2(1:3)'

>>
z2(1:2:6)'

>> z2(3:1:1)'

>> z2([1 2 5
6])'

ans =

ans =

ans =

ans =

3
4
5

3
5
7

5
4
3

3
4
7
8

Arithmetic, relational, and logical


operations on vectors
>> x = [5:5:25]

>> y = [1:20:100]

x=

y=

25

10

15

20

21

41

if multiplying or
dividing elements of
one vector by those of
another, be sure to use
. before * or /

61

81

>> add = x' + y'

>> multiply = x' .* y'

>> compare = x' < y' >> both = y' < 50 & y' == 41

add =

multiply =

compare =

6
31
56
81
106

5
210
615
1220
2025

0
1
1
1
1

both =
0
0
1
0
0

Last words on vectors

Can perform any operations on vector subsets


Vectors are really just special instances of
matrices look what happens if I try to define a
new column vector m, using the row vectors y
and z I get a 2 x 5 matrix

>> z = [1:5]
z=
1

>> y = [1:20:100]

81

21

41

>> m = [z; y]

ans =

m=

62

y=
1

>> z(1:2) + y(4:5)

61

83

1
1

2
21

3 4 5
41 61 81

Matrices
matrix = m x n array of numbers, where m is
rows and n is columns

Defined and indexed (but with one more


dimension) the same way as vectors

>> M = [2 4 6; 1 3 5; 7 8 9]

>> M(3,2)

M=

ans =

2
1
7

4
3
8

6
5
9

>> M2 = [2:2:6; 1:2:5; 7:9]

>> M2(1,3)

M2 =

ans =

2
1
7

4
3
8

6
5
9

Matrices

colon operator denotes all rows or all columns

arithmetic, relational, and logical operations

>> M

>> M2 = floor(5.*randn(3))

>> M + M2

M=

M2 =

ans =

1
3
-7

6
5
8
>> 7M(:,1)
9
ans =
2
1
7

-5
3
-1

>> M2(3,:)
ans =
-7
2

2
6
2

3
4

-1
6

11
11
>> M2 <= M
ans =

-1

1
0

Matrices
Can be thought of in terms of vectors

>> x =[1:5]

>> M = [x; y; z]

x=

M=
1

>> y = floor(2.*randn(1,5))

1 2 3 4 5
-2 1 -1 -2 4
100 80 60 40
20

y=
-2

-1

-2

>> z = [100:-20:20]
z=
100
20

80

60

40

>> M = [x(1:2); y(4:5); z([2 5])]


M=
1
-2
80

2
4
20

A matrix of matrices
called cells in MATLAB, use curly brackets

>> c = {M M2 M+M2; M(:,1) M2(3,:) M2<M}


c=
[3x3 double]
[3x1 double]

>> c{2,2}
ans =
-7
2

-1

[3x3 double]
[1x3 double]

[3x3 double ]
[3x3 logical]

More complex data structures

cell array of matrices(any size/type), numerically


indexed using curly brackets and parentheses

>> c = cell(size(M))

>> c{2,2}

>> c{2,2}(3,2) = 99; c{2,2}

c=

ans =

ans =

[]
[]
[]

[]
[]
[]

[]
[]
[]

-6
2
1

-2
4
7

-6
-1
-6

>> c{2,2} = floor(5*randn(3,3))

>> c{2,2}(3,2)

c=

ans =
[]
[]
[]

[]
[3x3 double]
[]

[]
[]
[]

-6
2
1

-2 -6
4 -1
99 -6

More complex data structures cont

struct similar to cell but not indexed, access


elements through field names using dot, 'value'
arrays must all be same size

>> s = struct('type',{'big','little'},'color','red','x',{3
4})
s=
1x2 struct array with fields:
type
color
x

>> s.type

>> s.color

>> s.x

ans =

ans =

ans =

big

red

ans =

ans =

little

red

3
ans =
4

Graphics examples
>> x = 10.*randn(1,1000);
>> hist(x)

>> scatter(x, x.^2), xlabel('x'), ylabel('x squared'), ...


title('scatter plot')

>> hist(x,20), xlabel('random numbers'), ylabel('count')

Graphics examples
>> M
M=
1 2 3 4 5
-2 1 -1 -2 4
100 80 60 40
20
>> boxplot(M')

>> x = [1:10];
>> y = [10:-1:1];
>> figure
>> subplot(1,2,1), plot(x,y)
>> subplot(1,2,2), plot(x,y.^2)

Useful commands...

help - Display help text in command window


>> help general
whos - List current variables, long form
lookfor - Search all M-files for keyword
what List MATLAB related files
which - Locate functions and files
clear - Clear variables and functions from
memory (be careful)

pwd Print working directory


cd change working directory
dir List directory contents
path - Get/set search path
addpath - Add directory to search path
pathtool - View, modify, and save the MATLAB
search path

save - Save workspace variables to disk

load - Load workspace variables from disk

diary - Save text of MATLAB session

isa - True if object is a given class

struct - Create or convert to structure array

type List contents of m-file

Vous aimerez peut-être aussi