2015F001/homework 9

From CDS 130

Jump to: navigation, search

Contents

  1. Solve for this MATLAB code without running it, then check your answer on MATLAB
  2. Does this MATLAB code produce an error when run? If so, why?
  3. Print out the answer for A
  4. Predict the answer of the following MATLAB code
  5. What’s the resulting value for counter?
  6. Nested for-loops and if-statements
  7. If statements
  8. Relational operators
  9. Logical Operators
  10. Predict the output of the following MATLAB code:
  11. Study the following Matlab code. Then describe the image that is produced by the code. Include information on size of image, colorbar, and sequence of colors.

1. Solve for this MATLAB code without running it, then check your answer on MATLAB

 
   A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
   B = [1, -2, 2; 2, -2, 3; 3, -3, 4];
   C(3,3) = 0;
   for m = 1:3
      for n = 1:3
         C(m,n) = A(m,n) + B(m,n);
         if C(m,n) < 0
            C(m,n) = C(m,n)*(-1);
         end
      end
   end
   C

2. Does this MATLAB code produce an error when run? If so, why?

    clear, clc;
    A = [1:1:4];
    B = [10:-2:4];
    C(1,4) = 0;
    for m = [4,3,2,1]
         if A(m) > 2
           A(1,m) = A(1,m)*m;
           B(1,m) = 2*B(1,m-1);
         else if A(m) < 2
           A(1,m) = A(1,m)*(-1 *m);
           B(1,m) = -2*B(1,m-1);
         end
    end
    C = A + B

3. Print out the answer for A

A(10) = 0.0;
for i = [1:10]
  if (mod(i, 3)==0)
    A(i) = 1;
  else 
    A(i) = 2; 
  end
end
A

4. Predict the answer of the following MATLAB code

   clear, clc;
   for  m = [1:2]       
       for n = [5:-1:2]   
          if ((m+n <= 5) && (m >= 5))
              disp(‘Condition 1 entered’)
          elseif ((m+n <= 4) || (m >= 4))
              disp(‘Condition 2 entered’)
          elseif ((m+n <= 3) && (m >= 3))
              disp(‘Condition 3 entered’)
          elseif ((m+n >= 2) || (m <= 2))
              disp(‘Condition 4 entered’)
          end
       end
   end

5. What’s the resulting value for counter?

  counter = 0;
  for i = 1:3
     for j = 1:3
         M(i, j) = 2*i+j;
         if (M(i, j) >3)
             M(i, j) = 2;
             counter = counter +1;
         end
     end
  end
  counter

6. Nested for-loops and if-statements

M =

   11    12    13    14     15
   12     13   14     15    16
   13    14     15    16    17
   14     15    16    17   18
   15    16    17    18     19

Using a set of nested for-loops and one or more if-statements, create and modify the matrix above so that all values along the diagonal are zero.

M =

    0    12    13    14     0
   12     0    14     0    16
   13    14     0    16    17
   14     0    16     0    18
    0    16    17    18     0

7. If statements

Rewrite the following statements to use only one if statement:

 if x < y 
    if  z < 10 
       w = x*y*z
    end
 end 

8. Relational operators

Suppose x = 6, without using Matlab, find the results of variable z in the following question.

 
(a) z = (x ~=6)
(b) z = x > 3 + 8
(c) z = 3 > 2 == 1
(d) z = 3 > 2 < 3
(e) z = 2 < 3 + 1 < 5 + 1

9. Logical Operators

Suppose x =[ -1, 0, 3] and y = [0, 3, 1], work out the results of the following expression before checking them in Matlab.

(a) ~x; 
(b) x & y
(c) x | y
(d) x > 0 & y > 0
(e) x + (~y) 

10. Predict the output of the following MATLAB code:

  clear; clc; 
  a = 3;
  b = 2;
  for m = [1:3]
    for n = [1:3]
      if (n > m)
         a = b^2 - a;
      elseif (n < m)
         b = 3*a;
      elseif (n == m)
         b = a - b;
      end
    end
   end
  a
  b

11. Study the following Matlab code. Then describe the image that is produced by the code. Include information on size of image, colorbar, and sequence of colors.

   clear, clc;
   mymap = [0,1,0; 1,1,1; 1,0,1];
   A(5,5) = 0;
   for m = [1:5]
     for n = [1:5]
        if (mod((m-n),3) == 0)
           A(m,n) = 1;
       elseif (mod((m-n),3) == 1)
           A(m,n) = 0;
       else
           A(m,n) = 0.5;
       end
     end
   end
   colormap(mymap)
   imagesc(A)
   colorbar
Personal tools