Kurz: 3 4 9
Teil des Kurses MATLAB
Text
s = 'Hallo Welt';
length(s)
upper(s)
lower(s)
s(1:5)
s(end:-1:1)
[s ' und Octave']
strcat('a', 'b', 'c')
strrep(s, 'Welt', 'MATLAB')
strfind(s, 'l')
strsplit('a,b,c', ',')
strjoin({'x', 'y', 'z'}, '-')
strtrim(' getrimmt ')
strcmp('abc', 'abc')
strcmpi('ABC', 'abc')
num2str(3.14159)
num2str(42)
str2num('7 + 1')
str2double('2.5')
sprintf('%05.1f|%-5s|%d', 3.14159, 'ab', 42)
regexprep('a1b22', '\d+', '#')
regexp('Preis: 12 und 34', '\d+', 'match')
double('A')
char(72, 105)
char([72 105])
isspace('a b')
fliplr('abc')
strcmp({'a', 'b', 'a'}, 'a')
upper({'x', 'y'})
numel(sprintf('%d', 12345))Ausgabe:
ans = 10
ans = HALLO WELT
ans = hallo welt
ans = Hallo
ans = tleW ollaH
ans = Hallo Welt und Octave
ans = abc
ans = Hallo MATLAB
ans =
3 4 9
ans =
{
[1,1] = a
[1,2] = b
[1,3] = c
}
ans = x-y-z
ans = getrimmt
ans = 1
ans = 1
ans = 3.1416
ans = 42
ans = 8
ans = 2.5000
ans = 003.1|ab |42
ans = a#b#
ans =
{
[1,1] = 12
[1,2] = 34
}
ans = 65
ans =
H
i
ans = Hi
ans =
0 1 0
ans = cba
ans =
1 0 1
ans =
{
[1,1] = X
[1,2] = Y
}
ans = 5Cell Arrays
Ein Cell Array ({ }) speichert Elemente beliebigen Typs und beliebiger Größe:
c = {1, 'Text', [1 2 3], {4, 5}}
class(c)
size(c)
c{2} % geschweifte Klammern: Inhalt
class(c{3})
class(c(3)) % runde Klammern: wieder ein Cell
c{3}(2)
c{end + 1} = 'neu';
numel(c)
namen = {'Mia', 'Tom', 'Zoe'};
laengen = cellfun(@length, namen)
grossbuchstaben = cellfun(@upper, namen, 'UniformOutput', false)
sortiert = sort(namen)
ismember('Tom', namen)
find(strcmp(namen, 'Zoe'))
cell2mat({1 2; 3 4})
num2cell([1 2 3])Ausgabe:
c =
{
[1,1] = 1
[1,2] = Text
[1,3] =
1 2 3
[1,4] =
{
[1,1] = 4
[1,2] = 5
}
}
ans = cell
ans =
1 4
ans = Text
ans = double
ans = cell
ans = 2
ans = 5
laengen =
3 3 3
grossbuchstaben =
{
[1,1] = MIA
[1,2] = TOM
[1,3] = ZOE
}
sortiert =
{
[1,1] = Mia
[1,2] = Tom
[1,3] = Zoe
}
ans = 1
ans = 3
ans =
1 2
3 4
ans =
{
[1,1] = 1
[1,2] = 2
[1,3] = 3
}Strukturen (struct)
Eine Struktur gruppiert benannte Felder:
person.name = 'Mia';
person.alter = 17;
person.hobbys = {'Lesen', 'Judo'};
person
person.alter = person.alter + 1;
disp(person.alter)
fieldnames(person)'
isfield(person, 'name')
isfield(person, 'stadt')
person = rmfield(person, 'hobbys');
fieldnames(person)'
feld = 'alter';
person.(feld) % dynamischer Feldname
leute(1).name = 'Mia'; leute(1).alter = 17;
leute(2).name = 'Tom'; leute(2).alter = 25;
leute(3).name = 'Zoe'; leute(3).alter = 31;
numel(leute)
{leute.name}
[leute.alter]
mean([leute.alter])
leute([leute.alter] > 20).name
s = struct('x', {1, 2, 3}, 'y', 'gleich');
size(s)
arrayfun(@(p) numel(p.name), leute)Ausgabe:
person =
scalar structure containing the fields:
name = Mia
alter = 17
hobbys =
{
[1,1] = Lesen
[1,2] = Judo
}
18
ans =
{
[1,1] = name
[1,2] = alter
[1,3] = hobbys
}
ans = 1
ans = 0
ans =
{
[1,1] = name
[1,2] = alter
}
ans = 18
ans = 3
ans =
{
[1,1] = Mia
[1,2] = Tom
[1,3] = Zoe
}
ans =
17 25 31
ans = 24.333
ans = Tom
ans = Zoe
ans =
1 3
ans =
3 3 3Maps und Tabellen
Für Schlüssel/Wert-Paare gibt es containers.Map:
m = containers.Map();
m('Mia') = 17;
m('Tom') = 25;
m('Zoe') = 31;
m.Count
m('Tom')
isKey(m, 'Ben')
keys(m)
values(m)
m.remove('Tom');
m.keys()
zaehler = containers.Map('KeyType', 'char', 'ValueType', 'double');
for wort = strsplit('a b a c a b', ' ')
w = wort{1};
if isKey(zaehler, w)
zaehler(w) = zaehler(w) + 1;
else
zaehler(w) = 1;
end
end
cellfun(@(k) sprintf('%s:%d', k, zaehler(k)), keys(zaehler), 'UniformOutput', false)Ausgabe:
ans = 3
ans = 25
ans = 0
ans =
{
[1,1] = Mia
[1,2] = Tom
[1,3] = Zoe
}
ans =
{
[1,1] = 17
[1,2] = 25
[1,3] = 31
}
ans =
{
[1,1] = Mia
[1,2] = Zoe
}
ans =
{
[1,1] = a:3
[1,2] = b:2
[1,3] = c:1
}In MATLAB gibt es zusätzlich den Datentyp table (readtable, T.Spalte), in Octave über das Paket dataframe oder Strukturen nachbildbar.
Merke
- Text steht in
'...';strcat,strsplit,strjoin,strrep,regexprep,sprintf - Cell Arrays
{...}speichern Verschiedenes;c{i}Inhalt,c(i)Teil-Cell structmit Feldern (s.name), Struct-Arrays und dynamische Felders.(name)containers.Mapfür Schlüssel/Wertcellfun/arrayfunwenden Funktionen elementweise an
Übungsaufgabe
Lege ein Struct-Array mit drei Personen an und gib den Altersdurchschnitt aus.
Quiz zur Selbstkontrolle
Was liefert c{2} bei einem Cell Array?
- Den Inhalt des zweiten Elements (richtig)
- Ein Cell mit dem zweiten Element
- Die zweite Zeile
- Die Länge
Wie greift man auf ein Feld mit dynamischem Namen zu?
- s.(name) (richtig)
- s[name]
- s->name
- getfield nur
Wofür ist containers.Map?
- Schlüssel/Wert-Zuordnungen (richtig)
- Landkarten
- Matrizen
- Zufallszahlen
Weiter im Kurs
Zurück: Kontrollfluss und Funktionen
Weiter: Numerik, Statistik und Datenanalyse
Alle Kapitel: MATLAB im Überblick