% ECE 1250 S12 Lecture M8 %------------------------------------------------------------------------------- % 3-D plots example using meshgrid() %------------------------------------------------------------------------------- % Rough notes. (Cleaner version will be posted later.) [xx,yy]=meshgrid(1:4,1:5) xx = 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 yy = 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 z = cos(2*pi*(3*xx-4*yy)*0.1) z = 0.8090 0.3090 -1.0000 0.3090 -1.0000 0.3090 0.8090 -0.8090 0.8090 -0.8090 -0.3090 1.0000 -0.3090 1.0000 -0.3090 -0.8090 -0.3090 -0.8090 0.8090 0.3090 surfl(xx,yy,z) %------------------------------------------------------------------------------- % Random numbers--a reminder %------------------------------------------------------------------------------- A = rand(3,4) % The rand() function generates numbers uniform between 0 and 1 A = 0.8147 0.9134 0.2785 0.9649 0.9058 0.6324 0.5469 0.1576 0.1270 0.0975 0.9575 0.9706 A = randn(3) % The randn() function generates normally distributed random #'s % with standard deviation = 1. (Bell curve) A = 0.2730 -0.4319 0.5875 -0.4326 -0.6097 0.4596 0.1270 0.0795 -0.5957 % When functions that create matrices have only one argument, they create square % matrices. %------------------------------------------------------------------------------- % And and Or operators %------------------------------------------------------------------------------- % Logical operators. % > < >= <= == ~= % We use And and Or operators to create compound logical comparisons. % scalar logic: and = &&, or = || (Will not be discussed further.) % compound operators: and = &, or = | % Create some matrices for the examples. A = [1,2;3,5] A = 1 2 3 5 B = [2,2;1,5] B = 2 2 1 5 % A nonzero value is considered True. Here is the Or operator in action. A | B ans = 1 1 1 1 % Here, we find out if a comparison is true in the == case OR in the < case: A == B | A < B ans = 1 1 0 1 % Note, we have an element-by-element logical OR of the results of the == and <: A == B ans = 0 1 0 1 A < B ans = 1 0 0 0 % If there is a 1 in a given position of either result, we get a 1 in that % position in the A == B | A < B comparison. % Here is the And operation. There is no position in the comparison matrices % where both matrices have a 1. A == B & A < B ans = 0 0 0 0 %------------------------------------------------------------------------------- % Any and All operators %------------------------------------------------------------------------------- % Recall that we can use the Any and All functions to determine the outcome of % a comparison. These functions are similar to And and Or operators. any(A == B) ans = 0 1 any(any(A==B)) ans = 1 all(A == B) ans = 0 1 % Processes cols %------------------------------------------------------------------------------- % If else control flow %------------------------------------------------------------------------------- % If statements allow us to change the instructions we execute based on values % of variables. % In the following example, the display() will only be executed if Every entry % of A < 3 is True (equal to 1). if A < 3 display('A < 3 was TRUE') end % Note that every "if" must be closed by an "end" statement. % Note that we may have multiple lines of code between the "if" and "end". % The above construct is equivalent to the following statements. if any(any(A<3)) display('A < 3 was TRUE') end A < 3 was TRUE A<3 ans = 1 1 0 0 % In the following example, the second display() (in the "else" will be executed % if the "if" condition fails. if A>0 display('A > 0 was TRUE') else display('not A > 0') end % Output: A > 0 was TRUE The else is optional. if A < 3 display('A < 3 was TRUE') end % Output: (no output since A < 3 did not produce all 1's) % Can also do if ... elseif ... elseif ... else ... end % In such a construct, the successive conditions are checked. If none of % them is true, the final "else" is executed. if A == eye(2) display('A is a 2x2 identity matrix.') % Indent to show level of if elseif A == zeros(2) display('A is a 2x2 zeros matrix.') A = eye(2); % Put any code you like inside if statements. else display('A is neither zeros nor identity.') A = zeros(2); end