prgramming chapter work
This commit is contained in:
parent
124eff5847
commit
9a36aed5f0
14
programming/lectures/control_structures.tex
Normal file
14
programming/lectures/control_structures.tex
Normal file
@ -0,0 +1,14 @@
|
||||
\section{Kontrollstrukturen}
|
||||
|
||||
\begin{definition}[Kontrollstrukturen]
|
||||
In der Regel wird ein Programm Zeile f\"ur Zeile von oben nach unten
|
||||
ausgef\"uhrt. Manchmal muss der Kontrollfluss aber so gesteuert
|
||||
werden, dass bestimmte Teile des Programmcodes wiederholt oder nur
|
||||
unter bestimmten Bedingungen ausgef\"uhrt werden. Von grosser
|
||||
Bedeutung sind hier zwei Strukturen:
|
||||
\begin{enumerate}
|
||||
|
||||
\item Schleigen.
|
||||
\item Bedingte Anweisungen und Verzweigungen.
|
||||
\end{enumerate}
|
||||
\end{definition}
|
372
programming/lectures/programming-chapter.tex
Normal file
372
programming/lectures/programming-chapter.tex
Normal file
@ -0,0 +1,372 @@
|
||||
\documentclass[12pt]{report}
|
||||
|
||||
%%%%% title %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\title{\tr{Introduction to Scientific Computing}{Einf\"uhrung in die wissenschaftliche Datenverarbeitung}}
|
||||
\author{Jan Benda\\Abteilung Neuroethologie\\[2ex]\includegraphics[width=0.3\textwidth]{UT_WBMW_Rot_RGB}}
|
||||
\date{WS 15/16}
|
||||
|
||||
%%%% language %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% \newcommand{\tr}[2]{#1} % en
|
||||
% \usepackage[english]{babel}
|
||||
\newcommand{\tr}[2]{#2} % de
|
||||
\usepackage[german]{babel}
|
||||
|
||||
%%%%% packages %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage{pslatex} % nice font for pdf file
|
||||
\usepackage[breaklinks=true,bookmarks=true,bookmarksopen=true,pdfpagemode=UseNone,pdfstartview=FitH,colorlinks=true,citecolor=blue]{hyperref}
|
||||
|
||||
%%%% layout %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage[left=25mm,right=25mm,top=20mm,bottom=30mm]{geometry}
|
||||
\setcounter{tocdepth}{1}
|
||||
|
||||
%%%%% section style %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage[sf,bf,it,big,clearempty]{titlesec}
|
||||
\setcounter{secnumdepth}{1}
|
||||
|
||||
|
||||
%%%%% units %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage[mediumspace,mediumqspace,Gray]{SIunits} % \ohm, \micro
|
||||
|
||||
|
||||
%%%%% figures %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage{subcaption}
|
||||
\usepackage{graphicx}
|
||||
\usepackage{xcolor}
|
||||
\pagecolor{white}
|
||||
|
||||
\newcommand{\ruler}{\par\noindent\setlength{\unitlength}{1mm}\begin{picture}(0,6)%
|
||||
\put(0,4){\line(1,0){170}}%
|
||||
\multiput(0,2)(10,0){18}{\line(0,1){4}}%
|
||||
\multiput(0,3)(1,0){170}{\line(0,1){2}}%
|
||||
\put(0,0){\makebox(0,0){{\tiny 0}}}%
|
||||
\put(10,0){\makebox(0,0){{\tiny 1}}}%
|
||||
\put(20,0){\makebox(0,0){{\tiny 2}}}%
|
||||
\put(30,0){\makebox(0,0){{\tiny 3}}}%
|
||||
\put(40,0){\makebox(0,0){{\tiny 4}}}%
|
||||
\put(50,0){\makebox(0,0){{\tiny 5}}}%
|
||||
\put(60,0){\makebox(0,0){{\tiny 6}}}%
|
||||
\put(70,0){\makebox(0,0){{\tiny 7}}}%
|
||||
\put(80,0){\makebox(0,0){{\tiny 8}}}%
|
||||
\put(90,0){\makebox(0,0){{\tiny 9}}}%
|
||||
\put(100,0){\makebox(0,0){{\tiny 10}}}%
|
||||
\put(110,0){\makebox(0,0){{\tiny 11}}}%
|
||||
\put(120,0){\makebox(0,0){{\tiny 12}}}%
|
||||
\put(130,0){\makebox(0,0){{\tiny 13}}}%
|
||||
\put(140,0){\makebox(0,0){{\tiny 14}}}%
|
||||
\put(150,0){\makebox(0,0){{\tiny 15}}}%
|
||||
\put(160,0){\makebox(0,0){{\tiny 16}}}%
|
||||
\put(170,0){\makebox(0,0){{\tiny 17}}}%
|
||||
\end{picture}\par}
|
||||
|
||||
% figures:
|
||||
\setlength{\fboxsep}{0pt}
|
||||
\newcommand{\texpicture}[1]{{\sffamily\footnotesize\input{#1.tex}}}
|
||||
%\newcommand{\texpicture}[1]{\fbox{\sffamily\footnotesize\input{#1.tex}}}
|
||||
%\newcommand{\texpicture}[1]{\setlength{\fboxsep}{2mm}\fbox{#1}}
|
||||
%\newcommand{\texpicture}[1]{}
|
||||
\newcommand{\figlabel}[1]{\textsf{\textbf{\large \uppercase{#1}}}}
|
||||
|
||||
% maximum number of floats:
|
||||
\setcounter{topnumber}{2}
|
||||
\setcounter{bottomnumber}{0}
|
||||
\setcounter{totalnumber}{2}
|
||||
|
||||
% float placement fractions:
|
||||
\renewcommand{\textfraction}{0.2}
|
||||
\renewcommand{\topfraction}{0.8}
|
||||
\renewcommand{\bottomfraction}{0.0}
|
||||
\renewcommand{\floatpagefraction}{0.5}
|
||||
|
||||
% spacing for floats:
|
||||
\setlength{\floatsep}{12pt plus 2pt minus 2pt}
|
||||
\setlength{\textfloatsep}{20pt plus 4pt minus 2pt}
|
||||
\setlength{\intextsep}{12pt plus 2pt minus 2pt}
|
||||
|
||||
% spacing for a floating page:
|
||||
\makeatletter
|
||||
\setlength{\@fptop}{0pt}
|
||||
\setlength{\@fpsep}{8pt plus 2.0fil}
|
||||
\setlength{\@fpbot}{0pt plus 1.0fil}
|
||||
\makeatother
|
||||
|
||||
% rules for floats:
|
||||
\newcommand{\topfigrule}{\vspace*{10pt}{\hrule height0.4pt}\vspace*{-10.4pt}}
|
||||
\newcommand{\bottomfigrule}{\vspace*{-10.4pt}{\hrule height0.4pt}\vspace*{10pt}}
|
||||
|
||||
% captions:
|
||||
\usepackage[format=plain,singlelinecheck=off,labelfont=bf,font={small,sf}]{caption}
|
||||
|
||||
% put caption on separate float:
|
||||
\newcommand{\breakfloat}{\end{figure}\begin{figure}[t]}
|
||||
|
||||
% references to panels of a figure within the caption:
|
||||
\newcommand{\figitem}[1]{\textsf{\bfseries\uppercase{#1}}}
|
||||
% references to figures:
|
||||
\newcommand{\panel}[1]{\textsf{\uppercase{#1}}}
|
||||
\newcommand{\fref}[1]{\textup{\ref{#1}}}
|
||||
\newcommand{\subfref}[2]{\textup{\ref{#1}}\,\panel{#2}}
|
||||
% references to figures in normal text:
|
||||
\newcommand{\fig}{Fig.}
|
||||
\newcommand{\Fig}{Figure}
|
||||
\newcommand{\figs}{Figs.}
|
||||
\newcommand{\Figs}{Figures}
|
||||
\newcommand{\figref}[1]{\fig~\fref{#1}}
|
||||
\newcommand{\Figref}[1]{\Fig~\fref{#1}}
|
||||
\newcommand{\figsref}[1]{\figs~\fref{#1}}
|
||||
\newcommand{\Figsref}[1]{\Figs~\fref{#1}}
|
||||
\newcommand{\subfigref}[2]{\fig~\subfref{#1}{#2}}
|
||||
\newcommand{\Subfigref}[2]{\Fig~\subfref{#1}{#2}}
|
||||
\newcommand{\subfigsref}[2]{\figs~\subfref{#1}{#2}}
|
||||
\newcommand{\Subfigsref}[2]{\Figs~\subfref{#1}{#2}}
|
||||
% references to figures within bracketed text:
|
||||
\newcommand{\figb}{Fig.}
|
||||
\newcommand{\figsb}{Figs.}
|
||||
\newcommand{\figrefb}[1]{\figb~\fref{#1}}
|
||||
\newcommand{\figsrefb}[1]{\figsb~\fref{#1}}
|
||||
\newcommand{\subfigrefb}[2]{\figb~\subfref{#1}{#2}}
|
||||
\newcommand{\subfigsrefb}[2]{\figsb~\subfref{#1}{#2}}
|
||||
|
||||
% references to tables:
|
||||
\newcommand{\tref}[1]{\textup{\ref{#1}}}
|
||||
% references to tables in normal text:
|
||||
\newcommand{\tab}{Tab.}
|
||||
\newcommand{\Tab}{Table}
|
||||
\newcommand{\tabs}{Tabs.}
|
||||
\newcommand{\Tabs}{Tables}
|
||||
\newcommand{\tabref}[1]{\tab~\tref{#1}}
|
||||
\newcommand{\Tabref}[1]{\Tab~\tref{#1}}
|
||||
\newcommand{\tabsref}[1]{\tabs~\tref{#1}}
|
||||
\newcommand{\Tabsref}[1]{\Tabs~\tref{#1}}
|
||||
% references to tables within bracketed text:
|
||||
\newcommand{\tabb}{Tab.}
|
||||
\newcommand{\tabsb}{Tab.}
|
||||
\newcommand{\tabrefb}[1]{\tabb~\tref{#1}}
|
||||
\newcommand{\tabsrefb}[1]{\tabsb~\tref{#1}}
|
||||
|
||||
|
||||
%%%%% equation references %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%\newcommand{\eqref}[1]{(\ref{#1})}
|
||||
\newcommand{\eqn}{\tr{Eq}{Gl}.}
|
||||
\newcommand{\Eqn}{\tr{Eq}{Gl}.}
|
||||
\newcommand{\eqns}{\tr{Eqs}{Gln}.}
|
||||
\newcommand{\Eqns}{\tr{Eqs}{Gln}.}
|
||||
\newcommand{\eqnref}[1]{\eqn~\eqref{#1}}
|
||||
\newcommand{\Eqnref}[1]{\Eqn~\eqref{#1}}
|
||||
\newcommand{\eqnsref}[1]{\eqns~\eqref{#1}}
|
||||
\newcommand{\Eqnsref}[1]{\Eqns~\eqref{#1}}
|
||||
|
||||
|
||||
%%%%% listings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage{listings}
|
||||
\lstset{
|
||||
inputpath=../code,
|
||||
basicstyle=\ttfamily\footnotesize,
|
||||
numbers=left,
|
||||
showstringspaces=false,
|
||||
language=Matlab,
|
||||
commentstyle=\itshape\color{red!60!black},
|
||||
keywordstyle=\color{blue!50!black},
|
||||
stringstyle=\color{green!50!black},
|
||||
backgroundcolor=\color{blue!10},
|
||||
breaklines=true,
|
||||
breakautoindent=true,
|
||||
columns=flexible,
|
||||
frame=single,
|
||||
caption={\protect\filename@parse{\lstname}\protect\filename@base},
|
||||
captionpos=t,
|
||||
xleftmargin=1em,
|
||||
xrightmargin=1em,
|
||||
aboveskip=10pt
|
||||
}
|
||||
|
||||
%%%%% math stuff: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage{amsmath}
|
||||
\usepackage{bm}
|
||||
\usepackage{dsfont}
|
||||
\newcommand{\naZ}{\mathds{N}}
|
||||
\newcommand{\gaZ}{\mathds{Z}}
|
||||
\newcommand{\raZ}{\mathds{Q}}
|
||||
\newcommand{\reZ}{\mathds{R}}
|
||||
\newcommand{\reZp}{\mathds{R^+}}
|
||||
\newcommand{\reZpN}{\mathds{R^+_0}}
|
||||
\newcommand{\koZ}{\mathds{C}}
|
||||
|
||||
|
||||
%%%%% structure: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage{ifthen}
|
||||
|
||||
\newcommand{\code}[1]{\texttt{#1}}
|
||||
|
||||
\newcommand{\source}[1]{
|
||||
\begin{flushright}
|
||||
\color{gray}\scriptsize \url{#1}
|
||||
\end{flushright}
|
||||
}
|
||||
|
||||
\newenvironment{definition}[1][]{\medskip\noindent\textbf{Definition}\ifthenelse{\equal{#1}{}}{}{ #1}:\newline}%
|
||||
{\medskip}
|
||||
|
||||
\newcounter{maxexercise}
|
||||
\setcounter{maxexercise}{9} % show listings up to exercise maxexercise
|
||||
\newcounter{theexercise}
|
||||
\setcounter{theexercise}{1}
|
||||
\newenvironment{exercise}[1][]{\medskip\noindent\textbf{\tr{Exercise}{\"Ubung}
|
||||
\arabic{theexercise}:}\newline \newcommand{\exercisesource}{#1}}%
|
||||
{\ifthenelse{\equal{\exercisesource}{}}{}{\ifthenelse{\value{theexercise}>\value{maxexercise}}{}{\medskip\lstinputlisting{\exercisesource}}}\medskip\stepcounter{theexercise}}
|
||||
|
||||
\graphicspath{{figures/}}
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\begin{document}
|
||||
|
||||
\chapter{Grundlagen der Programmierung in Matlab}
|
||||
|
||||
|
||||
\include{variables_datatypes}
|
||||
|
||||
|
||||
\include{vectors_matrices}
|
||||
|
||||
|
||||
\include{control_structures}
|
||||
|
||||
|
||||
\end{document}
|
||||
|
||||
|
||||
\end{document}
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\section{Statistics}
|
||||
What is "a statistic"? % dt. Sch\"atzfunktion
|
||||
\begin{definition}[statistic]
|
||||
A statistic (singular) is a single measure of some attribute of a
|
||||
sample (e.g., its arithmetic mean value). It is calculated by
|
||||
applying a function (statistical algorithm) to the values of the
|
||||
items of the sample, which are known together as a set of data.
|
||||
|
||||
\source{http://en.wikipedia.org/wiki/Statistic}
|
||||
\end{definition}
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\section{Data types}
|
||||
|
||||
\subsection{Nominal scale}
|
||||
\begin{itemize}
|
||||
\item Binary
|
||||
\begin{itemize}
|
||||
\item ``yes/no'',
|
||||
\item ``true/false'',
|
||||
\item ``success/failure'', etc.
|
||||
\end{itemize}
|
||||
\item Categorial
|
||||
\begin{itemize}
|
||||
\item cell type (``rod/cone/horizontal cell/bipolar cell/ganglion cell''),
|
||||
\item blood type (``A/B/AB/0''),
|
||||
\item parts of speech (``noun/veerb/preposition/article/...''),
|
||||
\item taxonomic groups (``Coleoptera/Lepidoptera/Diptera/Hymenoptera''), etc.
|
||||
\end{itemize}
|
||||
\item Each observation/measurement/sample is put into one category
|
||||
\item There is no reasonable order among the categories.\\
|
||||
example: [rods, cones] vs. [cones, rods]
|
||||
\item Statistics: mode, i.e. the most common item
|
||||
\end{itemize}
|
||||
|
||||
\subsection{Ordinal scale}
|
||||
\begin{itemize}
|
||||
\item Like nominal scale, but with an order
|
||||
\item Examples: ranks, ratings
|
||||
\begin{itemize}
|
||||
\item ``bad/ok/good'',
|
||||
\item ``cold/warm/hot'',
|
||||
\item ``young/old'', etc.
|
||||
\end{itemize}
|
||||
\item {\bf But:} there is no reasonable measure of {\em distance}
|
||||
between the classes
|
||||
\item Statistics: mode, median
|
||||
\end{itemize}
|
||||
|
||||
\subsection{Interval scale}
|
||||
\begin{itemize}
|
||||
\item Quantitative/metric values
|
||||
\item Reasonable measure of distance between values, but no absolute zero
|
||||
\item Examples:
|
||||
\begin{itemize}
|
||||
\item Temperature in $^\circ$C ($20^\circ$C is not twice as hot as $10^\circ$C)
|
||||
\item Direction measured in degrees from magnetic or true north
|
||||
\end{itemize}
|
||||
\item Statistics:
|
||||
\begin{itemize}
|
||||
\item Central tendency: mode, median, arithmetic mean
|
||||
\item Dispersion: range, standard deviation
|
||||
\end{itemize}
|
||||
\end{itemize}
|
||||
|
||||
\subsection{Absolute/ratio scale}
|
||||
\begin{itemize}
|
||||
\item Like interval scale, but with absolute origin/zero
|
||||
\item Examples:
|
||||
\begin{itemize}
|
||||
\item Temperature in $^\circ$K
|
||||
\item Length, mass, duration, electric charge, ...
|
||||
\item Plane angle, etc.
|
||||
\item Count (e.g. number of spikes in response to a stimulus)
|
||||
\end{itemize}
|
||||
\item Statistics:
|
||||
\begin{itemize}
|
||||
\item Central tendency: mode, median, arithmetic, geometric, harmonic mean
|
||||
\item Dispersion: range, standard deviation
|
||||
\item Coefficient of variation (ratio standard deviation/mean)
|
||||
\item All other statistical measures
|
||||
\end{itemize}
|
||||
\end{itemize}
|
||||
|
||||
\subsection{Data types}
|
||||
\begin{itemize}
|
||||
\item Data type selects
|
||||
\begin{itemize}
|
||||
\item statistics
|
||||
\item type of plots (bar graph versus x-y plot)
|
||||
\item correct tests
|
||||
\end{itemize}
|
||||
\item Scales exhibit increasing information content from nominal
|
||||
to absolute.\\
|
||||
Conversion ,,downwards'' is always possible
|
||||
\item For example: size measured in meter (ratio scale) $\rightarrow$
|
||||
categories ``small/medium/large'' (ordinal scale)
|
||||
\end{itemize}
|
||||
|
||||
\subsection{Examples from neuroscience}
|
||||
\begin{itemize}
|
||||
\item {\bf absolute:}
|
||||
\begin{itemize}
|
||||
\item size of neuron/brain
|
||||
\item length of axon
|
||||
\item ion concentration
|
||||
\item membrane potential
|
||||
\item firing rate
|
||||
\end{itemize}
|
||||
|
||||
\item {\bf interval:}
|
||||
\begin{itemize}
|
||||
\item edge orientation
|
||||
\end{itemize}
|
||||
|
||||
\item {\bf ordinal:}
|
||||
\begin{itemize}
|
||||
\item stages of a disease
|
||||
\item ratings
|
||||
\end{itemize}
|
||||
|
||||
\item {\bf nominal:}
|
||||
\begin{itemize}
|
||||
\item cell type
|
||||
\item odor
|
||||
\item states of an ion channel
|
||||
\end{itemize}
|
||||
|
||||
\end{itemize}
|
||||
|
165
programming/lectures/variables_datatypes.tex
Normal file
165
programming/lectures/variables_datatypes.tex
Normal file
@ -0,0 +1,165 @@
|
||||
\section{Variablen und Datentypen}
|
||||
|
||||
\subsection{Variablen}
|
||||
|
||||
Eine Variable ist ein Zeiger auf eine Stelle im Speicher. Dieser
|
||||
Zeiger hat einen Namen, den Variablennamen, und einen Datentyp
|
||||
(Abbildung \ref{variablefig}).Im Speicher wird der Wert der Variablen
|
||||
bin\"ar gespeichert. Wird auf den Wert der Variable zugegriffen, wird
|
||||
dieses Bitmuster je nach Datentyp interpretiert. Das Beispiel in
|
||||
Abbildung \ref{variablefig} zeigt, dass das gleiche Bitmuster im einen
|
||||
Fall als 8-Bit Integer Datentyp zur Zahl 38 interpretiert wird und im
|
||||
anderen Fall als Character zum kaufm\"annischen ``und'' ausgewertet
|
||||
wird. In Matlab sind Datentypen nicht von sehr zentraler
|
||||
Bedeutung. Wir werden uns dennoch sp\"ater etwas genauer mit ihnen
|
||||
befassen.
|
||||
|
||||
\begin{figure}
|
||||
\centering
|
||||
\begin{subfigure}{.5\textwidth}
|
||||
\includegraphics[width=0.8\textwidth]{images/variable}
|
||||
\label{variable:a}
|
||||
\end{subfigure}%
|
||||
\begin{subfigure}{.5\textwidth}
|
||||
\includegraphics[width=.8\textwidth]{images/variableB}
|
||||
\label{variable:b}
|
||||
\end{subfigure}
|
||||
\caption{\textbf{Variablen.} Variablen sind Zeiger auf eine Adresse
|
||||
im Speicher, die einen Namen und einen Datentypen beinhalten. Im
|
||||
Speicher ist der Wert der Variable bin\"ar gespeichert. Abh\"angig
|
||||
vom Datentyp wird dieses Bitmuster unterschiedlich
|
||||
interpretiert.}\label{variablefig}
|
||||
\end{figure}
|
||||
|
||||
|
||||
\subsection{Erzeugen von Variablen}
|
||||
In Matlab kann eine Variable auf der Kommandozeile, in einem Skript
|
||||
oder einer Funktion an beliebiger Stelle erzeugen. Das folgende
|
||||
Listing zeigt zwei M\"oglichkeiten:
|
||||
\footnotesize
|
||||
\begin{lstlisting}[label=varListing1, caption=Erzeugen von Variablen]
|
||||
>> y = []
|
||||
y =
|
||||
[]
|
||||
>>
|
||||
>> x = 38
|
||||
x =
|
||||
38
|
||||
\end{lstlisting}
|
||||
\normalsize
|
||||
|
||||
Die Zeile 1 kann etwa so gelesen werden:''Erzeuge eine Variable mit
|
||||
dem Namen y und weise ihr einen leeren Wert zu.'' Das
|
||||
Gleichheitszeichen ist der sogenannte
|
||||
\textit{Zuweisungsoperator}. Zeile 5 definiert eine Variable x, der
|
||||
nun der Zahlenwert 38 zugewiesen wird. Da Matlab, wenn nicht anders
|
||||
angegeben immer den ``double'' Datentypen benutzt, haben beide
|
||||
Variablen diesen Datentyp.
|
||||
|
||||
\footnotesize
|
||||
\begin{lstlisting}[label=varListing2, caption={Erfragen des Datentyps einer Variable, Listen aller definierten Variablen.}]
|
||||
>>disp(class(x))
|
||||
double
|
||||
>>
|
||||
>> who % oder whos um mehr Information zu bekommen
|
||||
\end{lstlisting}
|
||||
\normalsize
|
||||
|
||||
Bei der Namensgebung ist zu beachten, dass Matlab auf Gro{\ss}- und
|
||||
Kleinschreibung achtet und ein Variablennane mit einem alphabethischen
|
||||
Zeichen beginnen muss. Des Weiteren sind Umlaute, Sonder- und
|
||||
Leerzeichen in Variablennamen nicht erlaubt.
|
||||
|
||||
|
||||
\subsection{Arbeiten mit Variablen}
|
||||
|
||||
Nat\"urlich kann man mit den Variablen auch arbeiten, bzw
|
||||
rechnen. Matlab kennt alle normalen arithmetischen Operatoren wie
|
||||
\code{+, -, *. /}. Die Potenz wird \"uber das Dach Symbol \verb+^+
|
||||
dargestellt. Das folgende Listing zeigt, wie sie benutzt werden.
|
||||
|
||||
\footnotesize
|
||||
\begin{lstlisting}[label=varListing3, caption={Rechnen mit Variablen.}]
|
||||
>> x = 1;
|
||||
>> x + 10
|
||||
ans =
|
||||
11
|
||||
>>
|
||||
>> x % x wurde nicht veraendert
|
||||
ans =
|
||||
1
|
||||
>>
|
||||
>> y = 2;
|
||||
>>
|
||||
>> x + y
|
||||
ans =
|
||||
3
|
||||
>>
|
||||
>> z = x + y
|
||||
z =
|
||||
3
|
||||
>>
|
||||
>> z = z * 5;
|
||||
>> z
|
||||
z =
|
||||
15
|
||||
>>
|
||||
>> clear z
|
||||
\end{lstlisting}
|
||||
\normalsize
|
||||
|
||||
Beachtenswert ist z.B. in Zeilen 3 und 6, dass wir mit dem Inhalt
|
||||
einer Variablen rechnen k\"onnen, ohne dass dadurch ihr Wert
|
||||
ver\"andert w\"urde. Wenn der Wert einer Variablen ver\"andert werden
|
||||
soll, dann muss dieser der Variable expliyit zugewiesen werden (mit
|
||||
dem \code{=} Zuweisungsoperator, z.B. Zeilen 16, 20). Zeile 25 zeigt
|
||||
wie eine einzelne Variable gel\"oscht wird.
|
||||
|
||||
|
||||
\subsection{Datentypen}
|
||||
|
||||
Der Datentyp bestimmt, wie die im Speicher abgelegten Bitmuster
|
||||
interpretiert werden. Die Wichtigsten Datentpyen sind folgende:
|
||||
|
||||
\begin{itemize}
|
||||
\item \textit{integer} - Ganze Zahlen. Hier gibt es mehrere
|
||||
Unterarten, die wir in Matlab (meist) ignorieren k\"onnen.
|
||||
\item \textit{double} - Flie{\ss}kommazahlen.
|
||||
\item \textit{complex} - Komplexe Zahlen.
|
||||
\item \textit{logical} - Boolesche Werte, die als wahr
|
||||
(\textit{true}) oder falsch (\textit{false}) interpretiert werden.
|
||||
\item \textit{char} - ASCII Zeichen
|
||||
\end{itemize}
|
||||
|
||||
Unter den numerischen Datentypen gibt es verschiedene Arten mit
|
||||
unterschiedlichem Speicherbedarf und Wertebreich.
|
||||
|
||||
\begin{table}[]
|
||||
\centering
|
||||
\caption{Gel\"aufige Datentypen und ihr Wertebereich.}
|
||||
\label{dtypestab}
|
||||
\begin{tabular}{l|l|c|cl}
|
||||
Datentyp & Speicherbedarf & Wertebereich & Beispiel \\ \cline{1-4}
|
||||
double & 64 bit & $-2^{15} bis 2^{15}-1$ & Flie{\ss}kommazahlen.\\ \cline{1-4}
|
||||
int & 64 bit & $-2^{31} bis 2^{31}-1$ & Ganzzahlige Werte \\ \cline{1-4}
|
||||
int16 & 64 bit & $-2^{15} bis 2^{15}-1$ & Digitalisierte Spannungen. \\ \cline{1-4}
|
||||
uint8 & 64 bit & 0 bis 255 & Digitalisierte Imaging Daten. \\ \cline{1-4}
|
||||
& & &
|
||||
\end{tabular}
|
||||
\end{table}
|
||||
|
||||
|
||||
Matlab arbeitet meist mit dem ``double'' Datentyp wenn numerische
|
||||
Daten gespeichert werden. Dennoch lohnt es sich, sich ein wenig mit
|
||||
den DAtentypen auseinanderzusetzen. Ein Szenario, dass in der
|
||||
Neurobiologie nicht selten ist, ist, dass wir die elektrische
|
||||
Aktivit\"at einer Nervenzelle messen. Die gemessenen Spannungen werden
|
||||
mittels Messkarte digitalisiert und auf dem Rechner
|
||||
gespeichert. Typischerweise k\"onnen mit solchen Messkarten Spannungen
|
||||
im Bereich $\pm 10$\,V gemessen werden. Die Aufl\"osung der Wandler
|
||||
betr\"agt typischerweise 16 bit. Das heisst, dass der gesamte
|
||||
Spannungsbereich in $2^{16}$ Schritte aufgeteilt ist. Um Speicherplatz
|
||||
zu sparen ist es sinnvoll, die gemessenen Daten als ``int16'' Werte im
|
||||
Rechner abzulegen. Die Daten als ``echte'' Spannungen, also als
|
||||
Flie{\ss}kommawerte, abzulegen w\"urde den 4-fachen Speicherplatz
|
||||
ben\"otigen.
|
39
programming/lectures/vectors_matrices.tex
Normal file
39
programming/lectures/vectors_matrices.tex
Normal file
@ -0,0 +1,39 @@
|
||||
\section{Vektoren und Matrizen}
|
||||
|
||||
\begin{definition}[Vektoren und Matrizen]
|
||||
Vektoren und Matrizen sind die wichtigsten Datenstrukturen in
|
||||
Matlab. In andern Programmiersprachen spricht man von ein-
|
||||
bzw. mehrdimensionalen Feldern. Felder sind Datenstrukturen, die
|
||||
mehrere Werte des geleichen Datentyps in einer Variablen
|
||||
vereinen. Da Matalb seinen Ursprung in der Verarbeitung von
|
||||
mathematischen Vektoren und Matrizen hat werden sie hier auch so
|
||||
genannt.\\
|
||||
|
||||
In Wahrheit existiert auch in Matlab kein Unterschied zwischen
|
||||
beiden Datenstrukturen. Im Hintergrund sind auch Vektoren auch
|
||||
2-diemsensionale Matrizen bei denen einen Dimension dir Gr\"o{\ss}e
|
||||
1 hat.
|
||||
\end{definition}
|
||||
|
||||
|
||||
\subsection{Vektoren}
|
||||
|
||||
Im Gegensatz zu den Variablen, die einzelene Werte beinhalten,
|
||||
Skalare, kann ein Vektor mehrere Werte des gleichen Datentyps
|
||||
beinhalten (Abbildung \ref{vectorfig} B).
|
||||
|
||||
|
||||
\begin{figure}
|
||||
\includegraphics[width=0.8\columnwidth]{images/scalarArray}
|
||||
\caption{\textbf{Skalare und Vektoren. A)} Eine skalare Variable kann
|
||||
genau einen Wert tragen. \textbf{B)} Ein Vektor kann mehrer
|
||||
Werte des gleichen Datentyps (z.B. ganzzahlige Integer Werte)
|
||||
beinhalten. Matlab kennt den Zeilen- (row-) und Spaltenvektor
|
||||
(columnvector).}\label{vectorfig}
|
||||
\end{figure}
|
||||
|
||||
|
||||
\subsection{Unterscheidung Zeilen- und Spaltenvektoren}
|
||||
|
||||
|
||||
\subsection{Matrizen}
|
Reference in New Issue
Block a user