Matlab Einführung Tobias Wunner 16. Oktober 2006
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....
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...
Vorteile Modulorientiert / Offen Sämtliche Matlabskripte sind einsehbar >> edit sum
Vorteile Erweiterbar C oder FORTRAN Code durch mex Bibliotheken
Vorteile Matlab eine Einführung Profiler ab Version 7! Hilft beim Auffinden von Ressourcen-Verbrauchenden Programmteile
Vorteile Matlab eine Einführung Umfangreiche Softwarepakete vorhanden z.b.toolboxes Image Processing Toolbox Neural Network Toolbox www.mathworks.com/matlabcentral Hier findet ihr alles was ihr nicht findet :)
Nachteile Nicht Objektorientiert (kein JAVA) Effiziente Programme nur durch Vektorisierung >> x=1:5 X = 1 2 3 4 5 >> y=sqrt(1:5); statt for x=1:5 y = sqrt(x); end Umdenken
Nachteile Fast Alles ist erlaubt! Keine Variablendeklaration nötig Variablenneubelegungen (Typänderung) Funktionen mit gleichen Ein- und Ausgabeparametern >> x=2 >> x= super function x = myfun(x)... x = x*2;
Nachteile Fast Alles ist erlaubt! Sogar Build-In Matlab Funktionen können als Variablen genutzt werden => ACHTUNG tötet 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 % löschen von variablesum >> sum(tmp) % richtig! 15
MATLAB starten UNIX z.b. Remote via Putty andromeda@ option matlab andromeda@ matlab nojvm -nodisplay Windows
Handwerkzeug zum starten Arbeitsverzeichnis >> pwd >> ls >> cd projekt1 Variablen im Speicher >> who Variablen löschen/speichern >> clear y >> save dateiname >> load dateiname History Cursor
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)
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)
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
Vergleichsoperatoren kleiner/größer <,> gleich/ungleich ==,~= größergleich >= Logische Operatoren Und & Oder Nicht ~
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
Matrix Algebra Kronecker Produkt >> kron([1 1 1],[1 2 5]) 1 1 1 2 2 2 5 5 5 Weiter nützliche Matrixoperationen >> help matfun
Komponentenweise Operationen Inneres Produkt >> x=[1-1] (x ist hier >> x *x Spaltenvektor!) 2 Äußeres Produkt >> x*x 1-1 -1 1
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
Programmierung Schleifen for i=[1 4 2 1] sprinft( Round%d\n,i); end while BEDINGUNG... end
Programmierung Abbruch von Schleifen for i=1:10... if (error<.1) break; end end % bricht Schleife ab
Programmierung Bedingungen if BEDINGUNG... end if BEDINGUNG... else... end
Datentypen Arrays matrix = [ Peter, Hans ] PeterHans matrix = [ Peter ; Hans ] geht nicht! Lösung Cells cell = { Peter, Hans } m = char(cell) Peter Hans Richtig Indizieren >> m(2,:) Hans >> cell{2} Hans
Datentypen Cells -> beliebiege Datentypen cell2 = {12,cell,m} cell2{3} Peter Hans Ähnliche Idee bei Struct struct1.a = cell2; struct1.b = rand(10,10); struct1.a{2}{3}(2,:) % Zugriff auf Hans
Ein-/Ausgabe disp( Hello World ); sprinft( Zahl=%d\n,x); % Einfache Ausgabe % Formatierte Ausgabe x = input( Zahl eingeben:,x);% Einfache Eingabe S = 1 2 3 ; x = sscanf(s, %f ); % Formatierte Eingabe
Grafische Ausgabe von Daten neues Ausgabefenster öffnen figure Daten in einem 2D Bild ausgeben x=rand(10,10); x2=x*x ; figure, imagesc(x2), colorbar sc = scaled image Skala Kovarianzmatrix
Grafische Ausgabe von Daten Funktionen Plotten plot(xvalues,yvalues) xval = 0:.1:30 yval = sin(3.*cos(xval)) plot(xval,yval, r ) Mehrere Funktionen yval2 = sin(xval) plot(xval,yval,xval,yval2) oder plot(xval,yval, :b ) hold on
Grafische Ausgabe von Daten 3D Plot von Oberflächen (surfaces) x = imread(...); surf(x);shading flat;