Matlab Einfhrung
Tobias Wunner
16. Oktober 2006
Matlab eine Einfhrung
Vorteile Interpreter und interaktive Befehlseingabe
Schnelles Implementieren von wissenschaftlichen Methoden
Gutes Hilfesystem
>> lookfor 'sum'
TRACE Sum of diagonal elements.
CUMSUM Cumulative sum of elements.
SUM Sum of elements.
...
Matlab eine Einfhrung
Vorteile Interpreter und interaktive Befehlseingabe
Schnelles Arbeiten zum Entwickeln von Programmen
Gutes Hilfesystem
>> help sum
SUM Sum of elements.
S = SUM(X) is the sum of the elements of the
vector X. If X is a matrix, S is a row
vector with the sum over each
...
Matlab eine Einfhrung
Vorteile Modulorientiert / Offen
Smtliche Matlabskripte sind einsehbar
>> edit sum
Matlab eine Einfhrung
Vorteile Erweiterbar
C oder FORTRAN Code durch mexBibliotheken
Matlab eine Einfhrung Vorteile
Profiler ab Version 7! Hilft beim Auffinden von
Ressourcen-Verbrauchenden Programmteile
Matlab eine Einfhrung Vorteile
Umfangreiche Softwarepakete vorhanden z.B.Toolboxes
Image Processing Toolbox Neural Network Toolbox
www.mathworks.com/matlabcentral Hier findet ihr alles was ihr nicht findet :)
Matlab eine Einfhrung
Nachteile Nicht Objektorientiert (kein JAVA) Effiziente Programme nur durch
Vektorisierung
>> x=1:5
X =
1 2 3 4 5
>> y=sqrt(1:5);
for x=1:5
y = sqrt(x);
end
statt
Umdenken
Matlab eine Einfhrung
Nachteile Fast Alles ist erlaubt!
Keine Variablendeklaration ntig Variablenneubelegungen
(Typnderung)
Funktionen mit gleichen Ein- und Ausgabeparametern
>> x=2
>> x=super
function x = myfun(x)
...
x = x*2;
Matlab eine Einfhrung Nachteile
Fast Alles ist erlaubt! Sogar Build-In Matlab Funktionen knnen als
Variablen genutzt werden=> ACHTUNG ttet eingebaute Funktionen!!!
>> sum = 1:5;
>> sum([0 0 1 1 0])
3 4 % intuitiv erwartet 1+2+3+4+5=15
>> who % zeigt alle variablen im speicher an
ans sum
>> tmp = sum % sichern von variable sum
>> clear sum % lschen von variablesum
>> sum(tmp) % richtig!
15
Matlab eine Einfhrung
MATLAB starten UNIX
z.B. Remote via Putty
andromeda@ option matlab
andromeda@ matlab nojvm -nodisplay
Windows
Matlab eine Einfhrung
Handwerkzeug zum starten Arbeitsverzeichnis
>> pwd>> ls>> cd projekt1
Variablen im Speicher>> who
Variablen lschen/speichern>> clear y>> save dateiname>> load dateiname
HistoryCursor
Matlab eine Einfhrung
Matrizen Beliebige Matrizen
>> [1 3 5 7;2 4 6 8]
Spezielle Matrizen>> eye(3)
>> ones(2,4)
>> zeros(1,3)
Zufallszahlen>> rand(3)
>> rand(100,100)
Matlab eine Einfhrung
Matrizen indizieren Dimension
>> x=rand(3,4) % M-by-N Matrix
>> size(x,1) % M = Zeilen
>> size(x,2) % N = Spalten
Alle Elemente als Liste>> x(:)
k-tes bis Letztes Element>> y(k:end)
Matlab eine Einfhrung
Matrizen indizieren Mit Logik
>> x=2:7
2 3 4 5 6 7
>> x>4
0 0 0 1 1 1
Indizes ausgeben>> find(x>3)
5 6 7
Matlab eine Einfhrung
Vergleichsoperatoren kleiner/grer gleich/ungleich ==,~= grergleich >=
Logische Operatoren Und & Oder | Nicht ~
Matlab eine Einfhrung
Matrizen sortieren/umformen sortieren
>> x=6:-1:1
6 5 4 3 2 1
>> sort(x)
1 2 3 4 5 6
umformen>> reshape(x,2,3)
1 3 5
2 4 6
Matlab eine Einfhrung
Matrix Algebra Kronecker Produkt
>> kron([1 1 1],[1 2 5])
1 1 1
2 2 2
5 5 5
Weiter ntzliche Matrixoperationen>> help matfun
Matlab eine Einfhrung
Komponentenweise Operationen Inneres Produkt
>> x=[1 -1] (x ist hier
>> x*x Spaltenvektor!)
2
ueres Produkt>> x*x
1 -1
-1 1
Matlab eine Einfhrung
Vektormanipulationen Eintragsweise Operationen
>> [1 2 3].*[1 10 100]
1 20 300
>> [10 20 30]./[5 20 60]
2.0000 1.0000 0.5000
>> [2 4 8].^2
4 16 64
Matlab eine Einfhrung
Programmierung Schleifen
for i=[1 4 2 1]
sprinft(Round%d\n,i);
end
while BEDINGUNG
...
end
Matlab eine Einfhrung
Programmierung Abbruch von Schleifen
for i=1:10
...
if (error
Matlab eine Einfhrung
Programmierung Bedingungen
if BEDINGUNG
...
end
if BEDINGUNG
...
else
...
end
Matlab eine Einfhrung
Datentypen Arrays
matrix = [Peter,Hans]
PeterHans
matrix = [Peter;Hans]
geht nicht! Lsung
Cellscell = {Peter,Hans}
m = char(cell)
Peter
Hans
>> m(2,:)
Hans
>> cell{2}
Hans
Richtig Indizieren
Matlab eine Einfhrung
Datentypen Cells -> beliebiege Datentypen
cell2 = {12,cell,m}
cell2{3}
Peter
Hans
hnliche Idee bei Structstruct1.a = cell2;
struct1.b = rand(10,10);
struct1.a{2}{3}(2,:) % Zugriff auf Hans
Matlab eine Einfhrung
Ein-/Ausgabedisp(Hello World); % Einfache Ausgabe
sprinft(Zahl=%d\n,x); % Formatierte Ausgabe
x = input(Zahl eingeben:,x);% Einfache Eingabe
S = 1 2 3;
x = sscanf(S,%f); % Formatierte Eingabe
Matlab eine Einfhrung
Grafische Ausgabe von Daten neues Ausgabefenster ffnen
figure
Daten in einem 2D Bild ausgebenx=rand(10,10);
x2=x*x;
figure, imagesc(x2), colorbar
sc = scaled image Skala
Kovarianzmatrix
Matlab eine Einfhrung
Grafische Ausgabe von Daten Funktionen Plotten
plot(XVALUES,YVALUES)
xval = 0:.1:30
yval = sin(3.*cos(xval))
plot(xval,yval,r)
Mehrere Funktionenyval2 = sin(xval)
plot(xval,yval,xval,yval2)
oderplot(xval,yval,:b)
hold on
plot(xval,yval2,r)
Matlab eine Einfhrung
Grafische Ausgabe von Daten 3D Plot von Oberflchen (surfaces)
x = imread(...);
surf(x);shading flat;
Matlab eine Einfhrung