Matlab Einführung Tobias Wunner 16. Oktober 2006
Matlab eine Einführung 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 Einführung 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 Einführung Vorteile Modulorientiert / Offen Sämtliche Matlabskripte sind einsehbar >> edit sum
Matlab eine Einführung Vorteile Erweiterbar C oder FORTRAN Code durch mex Bibliotheken
Matlab eine Einführung Vorteile Profiler ab Version 7! Hilft beim Auffinden von Ressourcen-Verbrauchenden Programmteile
Matlab eine Einführung 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 Einführung Nachteile Nicht Objektorientiert (kein JAVA) Effiziente Programme nur durch Vektorisierung for x=1:5 y = sqrt(x); end >> x=1:5 X = 1 2 3 4 5 >> y=sqrt(1:5); statt „Umdenken“
Matlab eine Einführung 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;
Matlab eine Einführung 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 eine Einführung MATLAB starten UNIX z.B. Remote via Putty Windows andromeda@ option matlab andromeda@ matlab –nojvm -nodisplay
Matlab eine Einführung Handwerkzeug zum starten Arbeitsverzeichnis >> pwd >> ls >> cd projekt1 Variablen im Speicher >> who Variablen löschen/speichern >> clear y >> save dateiname >> load dateiname History Cursor
Matlab eine Einführung 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 Einführung 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 Einführung 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 Einführung Vergleichsoperatoren kleiner/größer <,> gleich/ungleich ==,~= größergleich >= Logische Operatoren Und & Oder | Nicht ~
Matlab eine Einführung 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 Einführung 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
Matlab eine Einführung Komponentenweise Operationen Inneres Produkt >> x=[1 -1]‘ (x ist hier >> x‘*x Spaltenvektor!) 2 Äußeres Produkt >> x*x‘ 1 -1 -1 1
Matlab eine Einführung 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 Einführung Programmierung Schleifen for i=[1 4 2 1] sprinft(´Round%d\n´,i); end while BEDINGUNG ...
Matlab eine Einführung Programmierung Abbruch von Schleifen for i=1:10 ... if (error<.1) break; % bricht Schleife ab end
Matlab eine Einführung Programmierung Bedingungen if BEDINGUNG ... end else
Matlab eine Einführung Datentypen Arrays matrix = [‘Peter‘,‘Hans‘] PeterHans matrix = [‘Peter‘;‘Hans‘] geht nicht! Cells cell = {‘Peter‘,‘Hans‘} m = char(cell) Peter Hans Lösung >> m(2,:) Hans >> cell{2}
Matlab eine Einführung 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
Matlab eine Einführung Ein-/Ausgabe disp(‘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 Einführung 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
Matlab eine Einführung 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 plot(xval,yval2,‘r‘)
Matlab eine Einführung Grafische Ausgabe von Daten 3D Plot von Oberflächen (surfaces) x = imread(...); surf(x);shading flat;
Matlab eine Einführung