Einführung in MATLAB
Grundlagen für die Übungen begleitend zur
Vorlesung „Neuroinformatik I”
Stefan Scherer ([email protected])David Bouchain ([email protected])19. 10. 2007Institut für NeuroinformatikFakultät für Ingenieurwissenschaften und Informatik
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 2
Inhalt
– Was ist MATLAB?
– Mit MATLAB arbeiten
– Variablen (Vektoren, Matrizen, ...)
– Flusskontrolle und Funktionen
– Visualisierung
– Nützliche Funktionen
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 3
Was ist MATLAB?
MATLAB ist:– eine Entwicklungsumgebung mit eigener Skriptsprache– spezialisiert auf numerische Berechnungen insbesondere mit
Vektoren und Matrizen– versehen mit vielseitigen Visualisierungsmöglichkeiten– ausgestattet mit einer sehr umfangreichen Funktionsbibliothek.
MATLAB ist nicht:– ein CAS (computer algebra system) wie z. B. MAPLE,
Mathematica– eine allgemeine Programmiersprache wie z. B. C/C++, Java– schnell!
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 4
Mit MATLAB arbeiten
MATLAB starten:– lokale Installation
• leider gibt es keine Lizenzen im SGI
– im SGI-Pool• Linux: matlab aufrufen
• Windows: Intallation vorhanden
– von einem entfernten Rechner aus• im Textmodus• via X11 forwarding
Anwenden:– interaktiv
– skriptbasiert (.m-Dateien)
>> x=[1,2,3]
x =
1 2 3
>> y=x'
y =
1 2 3
>> y*x
ans =
1 2 3 2 4 6 3 6 9
>>
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 5
Das MATLAB-Hauptfenster
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 6
Das Befehlsfenster
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 7
Der Editor
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 8
MATLAB übers Intranet/Internet
bouchain@retina:~$ ssh [email protected]:ab29@wsl02:~$ matlabWarning: Unable to open display , MATLAB is starting without a display. You will not be able to display graphics on the screen.
< M A T L A B > Copyright 1984-2006 The MathWorks, Inc. Version 7.3.0.298 (R2006b) August 03, 2006
To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com.
>> x=[1,2,3]
x =
1 2 3
>> exitab29@wsl02:~$ exitbouchain@retina:~$ ssh -Y [email protected]:ab29@wsl02:~$ matlabab29@wsl02:~$ exitbouchain@retina:~$
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 9
Allgemeines
Syntaktische Feinheiten:
– % beginnt Zeilenkommentar
– ; unterdrückt Konsolenausgabe
– gilt für Konsole und Skript!
>> x = 5
x =
5
>> y = x;>> y
y =
5
>> % y = 6>> y
y =
5
>>
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 10
Variablen
– Variablen müssen nicht deklariert werden!
– Bezeichner• sind alphanumerisch• beginnen mit einem Buchstaben
– MATLAB ist casesentitiv– Definition durch Zuweisung
• x = [1, 2, 3]
• s = 'Hello, World!'
– Operatoren• Zuweisung: x = y
• Arithmetik: +, -, *, /, ^
• Vergleich: >, <, >=, <=, ==, ~=
• Logik: &, |, ~
>> a??? Undefined function or variable 'a'.
>> a = 23
a =
23
>> A = 42
A =
42
>> a
a =
23
>> a ~= A
ans =
1>>
ungleich
NICHT
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 11
Vektoren
Definieren:
– Zeilenvektor:• x = [1, 2, 3]
• x = [1 2 3] identisch
• x = 7:11 <von>:<bis>
• x = 1:1.5:5 <von>:<Schritt>:<bis>
– Spaltenvektor• y = [1; 2; 3]
• y = [1 2 3]' transponieren
Kommata
Semikola
>> x = [1, 2, 3]
x =
1 2 3
>> x = 7:10
x =
7 8 9 10
>> x = 1:2:5
x =
1 3 5
>> y = [1 2 3]'
y =
1 2 3
>>
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 12
Vektoren (2)
Elementzugriff:
Beispielvektor x = [1 2 3 4 5]
– erstes Element: 1• x(1)
– letztes Element:• x(5)
• x(end)
• x(end+1) = 6 (wachsen)
– Teilvektor:• x(1:2:5) (ungerade Indizes)
>> x(1:2:5)>> x(1)
ans =
1
>> x(end)
ans =
5
>> x(end + 1) = 6
x =
1 2 3 4 5 6
>> x(1:2:5)
ans =
1 3 5
>>
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 13
Vektoren (3)
Elementweise Operationen:
Beispielvektoren x = [1 2 3] und y = x'
– Transponierung: y'
– Addition: x + y'
– Multiplikation: x .* y'
– Potenzierung: x .^ 2
– Subtraktion, Division, und Wurzel entsprechend
>> x = [1 2 3]; y = x';>> y'
ans =
1 2 3
>> x + y'
ans =
2 4 6
>> x .* y'
ans =
1 4 9
>> x .^ 2
ans =
1 4 9
>>
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 14
Vektoren (4)
Vektorielle Operationen:
Beispielvektoren x = [1 2 3] und y = x'
– Skalarprodukt: x * y
– Matrixprodukt: y * x
– Dimensionen müssen passen!
>> x = [1 2 3]; y = x';>> x * y
ans =
14
>> y * x
ans =
1 2 3 2 4 6 3 6 9
>> x(end + 1) = 0;>> x * y??? Error using ==> mtimesInner matrix dimensions must agree.
>>
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 15
Matrizen
Definieren:
– A = [1, 2, 3; 4, 5, 6]
Elementzugriff:
– direkt: A(2,3)
– letztes Element: A(end,end)
– Teilmatrix: A(2,1:2)
>> A = [1 2 3; 4 5 6]
A =
1 2 3 4 5 6
>> A(end, end)
ans =
6
>> A(2, 1:2)
ans =
4 5
>> A(1, :)
ans =
1 2 3
>>
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 16
Matrizen (2)
Operationen:
Beispielmatrix: A = [1, 2, 3; 4, 5, 6]
– Transponierung: A'
– Elementweise Multiplikation: A .* A
– Matrixmultiplikation: A' * A
>> A = [1 2 3; 4 5 6];>> A'
ans =
1 4 2 5 3 6
>> A .* A
ans =
1 4 9 16 25 36
>> A * A'
ans =
14 32 32 77
>>
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 17
Flusskontrolle
Bedingte Ausführung:
– if-Bedingung:
if <Bedingung 1><Anweisung 1>
elseif <Bedingung 2><Anweisung 2>
else<Anweisung 3>
end
– while-Schleife:
while <Bedingung><Anweisung>
end
>> x = 1; y = 0;>> if x ~= y'Foo!'else'Bar!'end
ans =
Foo!
>> x = 5;>> while x > yx = x - 1;end>> x
x =
0
>>
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 18
Flusskontrolle (2)
Iteratives Ausführen:
– for-Schleife
for <Laufvariable> = <Zeilenvektor><Anweisung>
end
• Änderung der Laufvariablen hat keinen Einfluss auf die Anzahl der Iterationen!
– for- und while-Schleifen können mit break verlassen werden.
>> for x = 1:3x = x - 2end
x =
-1
x =
0
x =
1
>> for x = 23:42break;xend>>
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 19
Funktionen
Eingebaute Funktionen
– eine Unmenge!– umfangreiche Dokumentation
• help <Funktion>• doc <Funktion>
– Beispiel:
>> help size SIZE Size of array. D = SIZE(X), for M-by-N matrix X, returns the two-element row vector D = [M,N] containing the number of rows and columns in the matrix. For N-D arrays, SIZE(X) returns a 1-by-N vector of dimension lengths. Trailing singleton dimensions are ignored. [M,N] = SIZE(X) for matrix X, returns the number of rows and columns in X as separate output variables. [M1,M2,M3,...,MN] = SIZE(X) for N>1 returns the sizes of the first N
>> x = [1 2 3]
x =
1 2 3
>> y = [1 2 3 4]
y =
1 2 3 4
>> size(x)
ans =
1 3
>> size(y'*x)
ans =
4 3
>>
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 20
Funktionen (2)
Benutzerdefinierte Funktionen:
– .m-Dateien
– vorangehender Kommentartext ist Hilfetext– Funktionsname ist Dateiname– Definition:
function <Ergebnis> = <Name> ( <Arg 1>, <Arg2>, ... )
– Beispiel:
% function_example%% "If the brain were so simple we could% understand it, we would be so% simple we couldn't."---Lyall Watson
function result = function_example(x, y)result = x * y;
>> help function_example function_example "If the brain were so simple we could understand it, we would be so simple we couldn't."---Lyall Watson
>> function_example([1 2], [1; 2])
ans =
5
>>
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 21
Übungsaufgaben
Bitte folgendes Format verwenden:
– jede Aufgabe als eigene Funktion
– eine .m-Datei beilegen, die alle Aufgaben aufruft
– Beispiel:
% Blatt 42
a1a([0,1,2],[1,2,3])a1b([0,1,2],[3,2,1])a2([0,1,2])
% a2% Aufgabe 2
function result = a2(x)result = x * x';
% a1a% Aufgabe 1a
function result = a1a(x, y)result = x + y;
% a1b% Aufgabe 1b
function result = a1b(x, y)result = x .* y;
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 22
Visualisierung
Funktionen:– plot: zeichnet eine Funktion– subplot(zeilen, spalten, nummer): mehrere Plots in einem Fenster– legend: stellt Legende dar (div. Optionen möglich)– title: fügt Titel hinzu– hold on bzw. off: nötig um Legende, Titel, mehrere Plots etc.
hinzuzufügen – …
Plotoptionen:– plot(x, y, [options]):
• Linien: ‘-’, ‘--’, ‘:’, ‘-.’• Markierungen: ‘x’, ‘o’, ‘*’, …• Farben: ‘r’, ‘b’, ‘g’, ‘c’, ‘y’, …
– Kombinationen möglich
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 23
Visualisierung (2)
Beispiel:
% fermi% Zeichnet eine Fermifunktion
function result = fermi()
% Sigmoid:x = -5:0.1:5;y = 1./(1+exp(-x));
% Plotten und nicht schliessen:plot(x,y);hold on;
% Ableitung:y = (1./(1 + exp(-x))).*(1 - (1./(1 + exp(-x))));
% Auch plotten:plot(x,y,'r');
% Bild speichern:print -dpng 'fermi.png'
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 24
Nützliche Funktionen
Ein paar hilfreiche Funktionen:
– size: Größe eines Vektors/einer Matrix
– find: gibt die Elemente zurück, die eine Bedingung erfüllen
– ones: Matrix der angegebenen Dimension, gefüllt mit Einsen
– diag: Diagonalmatrix
– clear all: alle Variablen löschen (jedes Programm damit beginnen!)
>> x = [-1 1 -1 -1 1];>> find(x >= 0)
ans =
2 5
>> x = ones(3)
x =
1 1 1 1 1 1 1 1 1
>> x = diag(1:3)
x =
1 0 0 0 2 0 0 0 3
>> clear all>> x??? Undefined function or variable 'x'.
>>
Einführung in MATLAB | Scherer & Bouchain | 19. 10. 2007Seite 25
Bitteschön!