for und while Schleifen, if Anweisungen
Viele Algorithmen benoetigen eine mehrfache Auswertung von Anweisungen.
Contents
Beispiel zur Wiederholung der for-Schleife: Fibonacci Folge
f = [1 1]; for n=3:10 f(n) = f(n-1) + f(n-2); end fprintf('Fibonacci Zahl f(10) = %d\n',f(10))
Fibonacci Zahl f(10) = 55
while Schleife
Wiederhole eine Folge von Anweisungen solange eine Bedingung erfuellt ist.
help while
WHILE Repeat statements an indefinite number of times.
The general form of a WHILE statement is:
WHILE expression
statements
END
The statements are executed while the real part of the expression
has all non-zero elements. The expression is usually the result of
expr rop expr where rop is ==, <, >, <=, >=, or ~=.
The BREAK statement can be used to terminate the loop prematurely.
For example (assuming A already defined):
E = 0*A; F = E + eye(size(E)); N = 1;
while norm(E+F-E,1) > 0,
E = E + F;
F = A*F/N;
N = N + 1;
end
See also FOR, IF, SWITCH, BREAK, CONTINUE, END.
Reference page in Help browser
doc while
Beispiel:
x = 10; while abs(x)>1 x = x/2; end fprintf('Ergebnis: x = %f\n',x)
Ergebnis: x = 0.625000
Nun wollen wir die Anzahl der benoetigten Iterationen zaehlen.
n=0; x=10; while abs(x)>1 x = x/2; n = n+1; end fprintf('Ergebnis nach %d Iterationen: x = %f\n',n,x);
Ergebnis nach 4 Iterationen: x = 0.625000
Es ist oft sinnvoll die Anzahl der Iterationen zu beschraenken.
n=0; x = 20000; while abs(x) > 1 && n <=20 x = x/2; n = n+1; end fprintf('Ergebnis nach %d Iterationen: x = %f\n',n,x)
Ergebnis nach 15 Iterationen: x = 0.610352
Logisches AND und OR
help &
& Logical AND.
A & B performs a logical AND of arrays A and B and returns an array
containing elements set to either logical 1 (TRUE) or logical 0
(FALSE). An element of the output array is set to 1 if both input
arrays contain a non-zero element at that same array location.
Otherwise, that element is set to 0. A and B must have the same
dimensions unless one is a scalar.
C = AND(A,B) is called for the syntax 'A & B' when A or B is an
object.
Note that there are two logical AND operators in MATLAB. The &
operator performs an element-by-element AND between matrices, while
the && operator performs a short-circuit AND between scalar values.
These operations are explained in the MATLAB Programming documentation
on logical operators, under the topic of Basic Program Components.
See also RELOP, OR, XOR, NOT.
Overloaded methods:
sym/and
Reference page in Help browser
doc and
help |
| Logical OR.
A | B performs a logical OR of arrays A and B and returns an array
containing elements set to either logical 1 (TRUE) or logical 0
(FALSE). An element of the output array is set to 1 if either input
array contains a non-zero element at that same array location.
Otherwise, that element is set to 0. A and B must have the same
dimensions unless one is a scalar.
C = OR(A,B) is called for the syntax 'A | B' when A or B is an
object.
Note that there are two logical OR operators in MATLAB. The |
operator performs an element-by-element OR between matrices, while
the || operator performs a short-circuit OR between scalar values.
These operations are explained in the MATLAB Programming documentation
on logical operators, under the topic of Basic Program Components.
See also RELOP, AND, XOR, NOT
Overloaded methods:
sym/or
Reference page in Help browser
doc or
if Anweisung
Im obigen Beispiel kann man die Anzahl der Iterationen auch durch eine IF-Anweisung beschraenken.
break beendet eine while oder for Schleife.
n=0; x = 20000; while abs(x) > 1 x = x/2; n = n+1; if n==10, break, end end fprintf('Ergebnis nach %d Iterationen: x = %f\n',n,x)
Ergebnis nach 10 Iterationen: x = 19.531250
help if
IF Conditionally execute statements.
The general form of the IF statement is
IF expression
statements
ELSEIF expression
statements
ELSE
statements
END
The statements are executed if the real part of the expression
has all non-zero elements. The ELSE and ELSEIF parts are optional.
Zero or more ELSEIF parts can be used as well as nested IF's.
The expression is usually of the form expr rop expr where
rop is ==, <, >, <=, >=, or ~=.
Example
if I == J
A(I,J) = 2;
elseif abs(I-J) == 1
A(I,J) = -1;
else
A(I,J) = 0;
end
See also RELOP, ELSE, ELSEIF, END, FOR, WHILE, SWITCH.
Reference page in Help browser
doc if
Beispiel: Vorsicht bei logischen Abfragen
x = cos(pi/3); if (x==0.5) fprintf('cos(pi/3)=0.5\n'); else fprintf('cos(pi/3) ist nicht 0.5!\n'); end fprintf('cos(pi/3)-0.5 = %16.8e\n',abs(x-0.5));
cos(pi/3) ist nicht 0.5! cos(pi/3)-0.5 = 1.11022302e-16
Logische Operatoren
Innerhalb eines Progamms testet man oft Bedingungen die den weiteren Programmfluss beeinflussen.
Beispiele: "Ist A gleich B?", "Ist A kleiner als B?", "Ist A<B und C<B?", "Ist A>B oder C>B?"
help relop
Relational operators.
< > Relational operators.
The six relational operators are <, <=, >, >=, ==, and ~=.
A < B does element by element comparisons between A and B
and returns a matrix of the same size with elements set to logical
1 (TRUE) where the relation is true and elements set to logical 0
(FALSE) where it is not. A and B must have the same dimensions
(or one can be a scalar).
& Element-wise Logical AND.
A & B is a matrix whose elements are logical 1 (TRUE) where both A
and B have non-zero elements, and logical 0 (FALSE) where either has
a zero element. A and B must have the same dimensions (or one can
be a scalar).
&& Short-Circuit Logical AND.
A && B is a scalar value that is the logical AND of scalar A and B.
This is a "short-circuit" operation in that MATLAB evaluates B only
if the result is not fully determined by A. For example, if A equals
0, then the entire expression evaluates to logical 0 (FALSE), regard-
less of the value of B. Under these circumstances, there is no need
to evaluate B because the result is already known.
| Element-wise Logical OR.
A | B is a matrix whose elements are logical 1 (TRUE) where either
A or B has a non-zero element, and logical 0 (FALSE) where both have
zero elements. A and B must have the same dimensions (or one can
be a scalar).
|| Short-Circuit Logical OR.
A || B is a scalar value that is the logical OR of scalar A and B.
This is a "short-circuit" operation in that MATLAB evaluates B only
if the result is not fully determined by A. For example, if A equals
1, then the entire expression evaluates to logical 1 (TRUE), regard-
less of the value of B. Under these circumstances, there is no need
to evaluate B because the result is already known.
~ Logical complement (NOT).
~A is a matrix whose elements are logical 1 (TRUE) where A has zero
elements, and logical 0 (FALSE) where A has non-zero elements.
xor Exclusive OR.
xor(A,B) is logical 1 (TRUE) where either A or B, but not both, is
non-zero. See XOR.
Beispiele:
true
ans =
1
false
ans =
0
x=5; disp(x>6);
0
disp(x<10 & x<6)
1
disp(x>0 | x < -1);
1
disp(xor(x<10,x>-1));
0
disp(xor(x<10,x<0));
1
disp(x>10 | false);
0
disp(x<10 & true);
1
Beachte: Angenommen wir wollen ueberpruefen, ob x=-3 zwischen -4 und 0 liegt. D.h. wir wollen pruefen ob -4 <=x <= 0 gilt.
x=-3; -4 <= x <= 0
ans =
0
Matlab interpretiert die obige Zeile von links nach rechts. Also zunaechst
-4 <= x
ans =
1
Das Ergebnis der ersten Abfrage (also 1) wird nun noch mit 0 verglichen.
(-4<=x) <= 0
ans =
0
Verwendung von Feldern in Bedingungsabfragen
Beispiel:
x = -1 + 2*rand(1,5); %1x5 Feld von Zufallszahlen in [-1,1] disp(x); if (x < 0) y = -10; else y = 10; end disp(y)
0.4121 -0.9363 -0.4462 -0.9077 -0.8057
10
Beachte: x<0 ist true, falls alle Komponenten von x kleiner Null sind.