%% Uebung 4 Kontrollstrukturen

clear 
close all

%% 1) for-Schleife

% a
for i = 0:10
    disp(i)
end

% b
for i = 10:-1:0
    disp(i)
end

% c
for i = 0:0.1:1
    disp(i)
end


%% 2) Zugriffe auf Vektoren mit der Laufvariable

% a

x = 1:100;

% b
for i = 1:length(x)
    disp(x(i))
end
% Die Laufvariable erhaelt mit jedem Iterationsschritt einen neuen Wert.
% Dieser "laeuft" von 1 bis zur Anzahl Element in x. Die Laufvar. wird dann
% als Index benutzt mit dem auf x zugegriffen wird.

% c
for i = x
    disp(i)
end
% Alternativ kann man mit der Schleife auch direkt durch den Vektor x
% laufen. Mit jeder Iteration enthaelt i nun einen Wert aus x. Es muss
% nicht mehr mittles Index auf x zugegriffen werden.

%% 3 ) Arithmetisches Mittel, Standardabweichung

x = rand(1,50) .* 10; % rand liefert im Bereich 0 bis 1

% a
mittelwert = [];
summe = 0;
n = length(x);

for i = 1:n
    summe = summe + x(i);
end
mittelwert = summe / n;
disp(mittelwert)

% b

% 1. Mittelwert berechnen
mittelwert = [];
summe = 0;
n = length(x);

for i = 1:n
    summe = summe + x(i);
end
mittelwert = summe / n;

% 2. Std berechnen
standardabw = [];
summe_quadratische_abw = 0;

for i = 1:n
    summe_quadratische_abw = summe_quadratische_abw + (x(i) - mittelwert).^2;
end
standardabw = sqrt(summe_quadratische_abw / n);
disp(standardabw)

% c
disp(mean(x))

disp(std(x,1)) % Wir verwenden eine Formel fuer die std, die in Matlab nicht default ist


%% 4 While Schleife

% a
count = 0;
while count < 100
    disp(count)
    count = count + 1;
end

% b
% count = 0;
% while true
%    disp(count)
%    count = count + 1;
% end


%% 5 
count = 1;
x = 1:10;
while true
    disp(x(count))
    count = count + 1;
end

%% 6 

% a
while true
    x = randn(1,1);
    if x > 1.33
        break;
    end
end

% b 
trials = zeros(1000,1);
for i = 1:length(trials)
    count = 1;
    while true
        x = randn(1,1);
        if x > 1.33
            trials(i) = count;
            break;
        end
        count = count + 1;
    end
end
disp(mean(trials))

% c 
figure()
plot(trials)
xlabel('Durchgang')
ylabel('Anzahl Versuche')

% d

% Mit groesser werdender Schwelle steigt auch die durchschnittliche Anzahl
% Versuche.

%% 7 Loeschen von Elementen

% a
x = rand(10,1) .* 10;
disp('vorher:')
disp(x)
for i = length(x):-1:1
    if x(i) < 5
        x(i) = [];
    end
end
disp('nachher:')
disp(x)
% Der Trick ist hier nicht von vorne durch den Vektor zu laufen, sondern
% rueckwaerts. Ansonsten laeuft man Gefahr hinter den gekuerzten Vektor zu
% greifen, was zu einem Index exceeds matrix dimensions Fehler fuehren
% wuerde.

% b
x = rand(10,1) .* 10;
disp('vorher:')
disp(x)
for i = length(x):-1:1
    if x(i) < 5 & x(i) > 2
        x(i) = [];
    end
end
disp('nachher:')
disp(x)

% c
x = rand(10,1) .* 10;
disp('vorher:')
disp(x)
x(x < 5 & x >2) = [];
disp('nachher:')
disp(x)


%% 8 Teste den Zufallsgenerator

x           = rand(1000,1); % some random numbers
count       = zeros(5,1);
for i=1:length(x)
    if(x(i) < 0.2)
        count(1) = count(1) + 1;
    elseif(x(i) < 0.4)
        count(2) = count(2) + 1;
    elseif(x(i) < 0.6)
        count(3) = count(3) + 1;
    elseif(x(i) < 0.8)
        count(4) = count(4) + 1;
    else
        count(5) = count(5) + 1;
    end
end
disp(count)
% Die Erwartung waere eine Gleichverteilung.

%% 9) String parsing

filename = '2015-10-12_100Hz_1.25V.dat';

% a
positions = [1];
for i = 1:length(filename)
    if filename(i) == '_'
        positions = cat(1, positions, i);
    end
end
positions = cat(1, positions, length(filename))
disp(positions)

for i = 2:length(positions)
    disp(char(filename(positions(i-1):positions(i))))
end