5. Contingencies

 

 

5.1 Using the if ... else ... end construct

5.2 Using the switch ... case, ... end construct

5.3 Using the for ... end construct

5.4 Using the while ... end construct and escaping from run-away loops

5.5 Vectorizing rather than using for ... end

5.6 If-ing instantly

5.7 If-ing instantly once again and finding indices of satisfying values

5.8 Practicing contingencies

 

 

top of page

5.1 Using the if ... else ... end construct

 

Code 5.1.1:

 

b = 2;

a = 1;

if a == 1

    b = 2 * b;

else

    b = -2 *b;

end       

b

 

Output  5.1.1:

 

b =

     4

 

Code 5.1.2:

 

b = 2;

a = -1;

if a == 1

    b = 2 * b;

end       

b

 

Output 5.1.2:

 

b =

2

    

 

Code 5.1.3:

 

b = 2;

a = 1;

if a < 0

    b = -1;

elseif a == 0

    b = 0;

elseif a == 1

    b = 1;

end

b

 

Output 5.1.3:

 

b =

1

 

Code 5.1.4:

 

b = 2;

a = 2.7;

if (a >= 1) & (a <= 3)

    b = 2 * b

end

 

Output 5.1.4:

 

b =

 

4

    

Code 5.1.5:

 

b = 2;

a = 3.7;

if (a <= 1) | (a >= 3)   

    b = -b;

end 

b

 

 

Output 5.1.5:

 

b =

 

-2

 

Code 5.1.6:

 

b = -2;

a = 3.7;

if a ~= 10

    b = -b;

end 

b

 

Output 5.1.6:

 

b =

 

2

 

Code 5.1.7:

 

A = -2.3

a = 10

if A <= 0

    if a <= -5

       b = 1;   %if A is <=0 and if a <=-5, b gets 1

    else

       b = 2;   %if A is <=0 and if a is not <=-5, b gets 2

    end

else

    if a <= 5   

       b = 3;    %if A is not <=0 and if a <=5, b gets 3

    else

       b = 4;    %if A is not <=0 and if a is not <=5, b gets 4

    end

end

b

 

Output 5.1.7:

 

A =

 

   -2.3000

 

a =

 

    10

 

b =

 

     2

 

 

 

top of page

5.2 Using the switch ... case ... end construct

 

 

Code 5.2.1:

 

x = 1;

switch x

    case 1

        y = -1;

    case 2

        y = -2*x;

    case 3

        y = -3*x;

    otherwise

        y = 0;

end

 

Output 5.2.1:

 

y =

 

     -1

 

 

Code 5.2.2:

 

% Days_In_A_Month

 

month = 4;    % April, but choose any month

year = 2006;  % 2006, but choose any year

 

switch month

    case {4, 6, 9, 11}

        no_of_days = 30;

    case 2

        if rem(year, 4) == 0 & ...

                (rem(year, 100) ~= 0 | rem(year, 400) == 0)

            % it’s a leap year

            no_of_days = 29;

        else

            no_of_days = 28;

        end

    otherwise

        no_of_days = 31;

end

 

[year month no_of_days]   % display result in matrix form

 

Output 5.2.2:

 

ans =

 

        2006           4          30

 

 

top of page

5.3 Using the for ... end construct

 

 

Code 5.3.1:

 

for i = 1:6   

    a = 2 * i

end

 

Output 5.3.1:

 

a =

 

     2

 

a =

 

     4

 

a =

 

     6

 

a =

 

     8

 

a =

 

    10

 

a =

 

    12

 

Code 5.3.2:

 

for i = 1:6   

    a(i) = 2 * i;

end

a

 

Output 5.3.2:

 

a =

 

     2     4     6     8    10    12

 

 

Code 5.3.3:

 

for i = 1:6

    for j = 1:3

        a(i,j) = i + j;

   end

end

a

 

 

 

Output  5.3.3:

 

a =

     2     3     4     8    10    12

     3     4     5     0     0     0

     4     5     6     0     0     0

     5     6     7     0     0     0

     6     7     8     0     0     0

     7     8     9     0     0     0

 

 

Code 5.3.4:

 

clear a

for i = 1:6

    for j = 1:3

        a(i,j) = i + j;

   end

end

a

 

Output 5.3.4:

 

a =

     2     3     4

     3     4     5

     4     5     6

     5     6     7

     6     7     8

     7     8     9

 

 

Code 5.3.5:

 

 for i = 0:10

    a(i) = i + 1;

 end

 

Output 5.3.5:

 

??? Subscript indices must either be real positive integers or logicals.

 

Error in ==> D:\Lab and Teach\PSU Teaching\Programming Seminar\Programming Seminar .m files\for_bad_index.m

On line 2  ==>     a(i) = i+1;

 

Code 5.3.6:

 

x = 10;

for i = -3:3

    a = x*i

end

 

Output 5.3.6:

 

a =

 

   -30

 

a =

 

   -20

 

a =

 

   -10

 

a =

 

     0

 

a =

 

    10

 

a =

 

    20

 

a =

 

    30

 

Code 5.3.7:

 

x = 10;

for i = -3:3

    if i ~= 0

        a = x/i

    end

end

 

Output 5.3.7:

 

a =

 

   -3.3333

 

a =

 

    -5

 

a =

 

   -10

 

a =

 

    10

 

a =

 

     5

 

a =

 

    3.3333

 

 

 

Code 5.3.8:

 

x = 10;

for i = -3:3

%    if i ~= 0

        a = x/i 

    end

end

a

 

Output 5.3.8:

 

a =

 

   -3.3333

 

a =

 

    -5

 

a =

 

   -10

 

Warning: Divide by zero.

(Type "warning off MATLAB:divideByZero" to suppress this warning.)

> In D:\Lab and Teach\PSU Teaching\Programming Seminar\Programming Seminar .m files\David_If_For_While_12.m at line 267

 

a =

 

   Inf

 

a =

 

    10

 

a =

 

     5

 

a =

 

    3.3333

 

a =

 

    3.3333

 

 

 

top of page

5.4 Using the while ... end construct and escaping from run-away loops

 

 

Code 5.4.1:

 

a = 1;

b = .25;

steps = 0;

while a < 10

   a = a + a^b;

   steps = steps + 1;

end

a

steps

 

Output 5.4.1:

 

a =

 

   10.9475

 

steps =

 

     7

 

 

Code  5.4.2:

 

a = .75;

need_to_keep_going = 1;

while need_to_keep_going == 1

      a = a^2

      if a > 1

          need_to_keep_going = 0;

      end

end

 

 

Code  5.4.3:

 

a = .75;

need_to_keep_going = 1;

allowed_steps = 5;

steps = 0;

while (steps < allowed_steps) & (need_to_keep_going == 1)

      a = a^2

      steps = steps + 1;

      if a > 1

          need_to_keep_going = 0;

      end

end

 

Output 5.4.3:

 

a =

 

    0.5625

 

a =

 

    0.3164

 

a =

 

    0.1001

 

a =

 

    0.0100

 

a =

 

  1.0045e-004

 

Code 5.4.4:

 

a = .75;

 need_to_keep_going = 1;

 step = 0;

 allowed_steps = 1000;

 while need_to_keep_going == 1

     a = a^2;

     if a > 1

         need_to_keep_going = 0;

     end

     step = step + 1;

     if step == allowed_steps

          break

     end 

 end

 a

 

Output 5.4.4:

 

a =

 

     0

 

step =

 

        1000

 

 

 

top of page

5.5 Vectorizing rather than using for … end

 

Code 5.5.1

 

clear all

 

n = 50000;

a = 2;

 

% Multiply using for … end

tic

for i = 1:n

    x(i) = a*i;

end

For_Time = toc

 

 

% Multiply using vectorizing

tic

y = a*[1:n];

Vectorize_Time = toc

 

 

Output 5.5.1

 

For_Time =

 

   19.3270

 

Vectorize_Time =

 

     0

 

 

top of page

5.6 If-ing instantly

 

 

Code 5.6.1

 

% generate normally distributed random sample

randn('state',sum(100*clock));

sample = randn(1,10);

random_sample = sample'  % print out transpose for easy reading

 

 

% identify values greater than the mean using for and if

top_half = [];

for i = 1:length(sample)

    if sample(i) > mean(sample)

        top_half = [top_half sample(i)];

    end

end

top_half

 

 

% Identify values greater than the mean using instant if-ing

truth_values_of_indices_satisfying_criterion = [sample > mean(sample)] 

new_top_half = sample(truth_values_of_indices_satisfying_criterion)

 

 Output 5.6.1

 

random_sample =

 

   -0.0190

    1.1116

    2.6756

   -0.4239

    1.5166

   -0.1902

    0.1163

    1.2005

    0.4585

   -0.5133

 

top_half =

 

    1.1116    2.6756    1.5166    1.2005

 

truth_values_of_indices_satisfying_criterion =

 

     0     1     1     0     1     0     0     1     0     0

 

new_top_half =

 

    1.1116    2.6756    1.5166    1.2005

 

 

top of page

5.7 If-ing instantly once again and finding indices of satisfying values

 

Code 5.7.1

 

h = randperm(11) + 10

(h == 12 | h == 16)

find(h == 12 | h == 16)

 

Output 5.7.1

 

h =

 

    12    16    15    19    21    11    20    18    14    13    17

 

ans =

 

     1     1     0     0     0     0     0     0     0     0     0

 

ans =

 

     1     2