Homework due Wednesday 4/9/14

From CDS 130

Jump to: navigation, search

Contents

  1. Create a new matrix
  2. Create a Matrix
  3. Create a Matrix
  4. Create a Matrix
  5. Write a Matlab command that will generate a 5 × 5 matrix filled with random integers between -100 and 100.
  6. Let x=(0:pi/2:2*pi). What Matlab commands will create a matrix whose first row is x, whose second row is the sine of each entry in x, and whose third row is the cosine of each entry in x?
  7. Matrix Operation
  8. Matrix Operation
  9. Matrix Calculation
  10. Load data to Matlab

1. Create a new matrix

What is the output of executing the following matlab code?

clear; clc; 
A = [1:10; 2:11; 3:12];
B = A(:, 5:8);
C = B.*2; 
C


Answer:

C =

   10    12    14    16
   12    14    16    18
   14    16    18    20

2. Create a Matrix

Create a 5x5 matrix and fill it initially with zeros. Next, store into matrix elements (1,3), (2,4), (3,5), (4,1) and (5,2) values equal to the row number plus the column number for that element. Print out the matrix. Finally, add 3 to every element of the matrix and save each result back into the matrix. Print out the matrix.


Matlab code:


>> A = zeros(5,5)

A =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0

>> A(1,3) = 4

A =

     0     0     4     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0

 A (2, 4) = 6

A =

     0     0     4     0     0
     0     0     0     6     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0

>> A(3,5) = 8;
>> A(4,1) = 5;
>> A(5,2) = 7

A =

     0     0     4     0     0
     0     0     0     6     0
     0     0     0     0     8
     5     0     0     0     0
     0     7     0     0     0

>> A + 3

ans =

     3     3     7     3     3
     3     3     3     9     3
     3     3     3     3    11
     8     3     3     3     3
     3    10     3     3     3

3. Create a Matrix

Create a 7 by 5 matrix with all elements initialized to 2.0.

Matlab code:

>> B = zeros(7,5)

B =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0

>> B + 2.0

ans =

     2     2     2     2     2
     2     2     2     2     2
     2     2     2     2     2
     2     2     2     2     2
     2     2     2     2     2
     2     2     2     2     2
     2     2     2     2     2

>>

4. Create a Matrix

Create the following matrix by using vector notation (i.e. the colon operator) for creating vectors with constant spacing. Do not type individual elements explicitly.

\mathbf{A} = \begin{bmatrix}

0 & 0.5 & 1.0 & 1.5 & 2.0 & 2.5 & 3.0 \\
60 & 50 & 40 & 30 & 20 & 10 & 0 \\
0 & 0.8333 & 1.6667 & 2.5 & 3.3333 & 4.16667 & 5 \\\end{bmatrix}.


Matlab code:

 C = [0:.5:3.0 ; 60:-10:0 ; 0:.8333:5]

C =

  Columns 1 through 6

         0    0.5000    1.0000    1.5000    2.0000    2.5000
   60.0000   50.0000   40.0000   30.0000   20.0000   10.0000
         0    0.8333    1.6666    2.4999    3.3332    4.1665

  Column 7

    3.0000
         0
    4.9998

5. Write a Matlab command that will generate a 5 × 5 matrix filled with random integers between -100 and 100.

 G = randi([-100,100],5,5)

G =

    63   -81   -69   -72    31
    82   -45    95   -16   -93
   -75     9    92    84    70
    83    92    -3    59    87
    27    93    60    92    36

 

6. Let x=(0:pi/2:2*pi). What Matlab commands will create a matrix whose first row is x, whose second row is the sine of each entry in x, and whose third row is the cosine of each entry in x?

 x=(0:pi/2:2*pi)

x =

         0    1.5708    3.1416    4.7124    6.2832

>> a1 = x

a1 =

         0    1.5708    3.1416    4.7124    6.2832

>> a2 = sin(x)

a2 =

         0    1.0000    0.0000   -1.0000   -0.0000

>> a3 = cos(x)

a3 =

    1.0000    0.0000   -1.0000   -0.0000    1.0000


 A = [a1;a2;a3]

A =

         0    1.5708    3.1416    4.7124    6.2832
         0    1.0000    0.0000   -1.0000   -0.0000
    1.0000    0.0000   -1.0000   -0.0000    1.0000
 

7. Matrix Operation

Set up any 3 x 3 matrix A with nonempty elements. Write some command-line statements to perform the following operations:

(a) Interchange column 2 and 3;

(b) Add a fourth column (of 0s);


Matlab code:

 A= rand(3,3)
 AA = A(:, [1, 3, 2])

A =

    0.0971    0.3171    0.4387
    0.8235    0.9502    0.3816
    0.6948    0.0344    0.7655


AA =

    0.0971    0.4387    0.3171
    0.8235    0.3816    0.9502
    0.6948    0.7655    0.0344

>> A = AA

A =

    0.0971    0.4387    0.3171
    0.8235    0.3816    0.9502
    0.6948    0.7655    0.0344

 A(3,4) = 0

A =

    0.0971    0.4387    0.3171         0
    0.8235    0.3816    0.9502         0
    0.6948    0.7655    0.0344         0

8. Matrix Operation

The following matrix is defined in MATLAB:

 \mathbf {N} =\begin{bmatrix}
33 & 21 & 9 & 14 & 30 \\
30 & 18 & 6 & 18 & 34 \\
27 & 15 & 6 & 22 & 38 \\
24 & 12 & 10 & 26 & 42 \\\end{bmatrix}.

By hand (pencil and paper) write what will be displayed in the following commands are executed by MATLAB. Check your answers by executing the commands with MATLAB

a. A=[N(1,1:4)’, N(2,2:5)’]

33 18
21 6
9  18
14 34


b. B=[N(:,3)’ N(3,:)]


9 6 6 10 27 15 6 22 38


c. C(3:4,5:6)=N(2:3,4:5)


0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 18 34
0 0 0 0 22 38

9. Matrix Calculation

Matlab has been widely used for matrix calculations. Assign the following matrix to variable A and B, respectively.

 \mathbf{A} = \begin{bmatrix}

1 & 2 \\
2 & 4 \\
3 & 6 \\\end{bmatrix}.
\mathbf{B} = \begin{bmatrix}

1 & 2 & 3\\
4 & 5 & 6\\\end{bmatrix}.


Use MATLAB to perform the following calculation, and record the MATLAB output below.

a) sum(A):

b) sum(sum(B));

c) Compute (A. * B')20

d) Compute (AB)20


Matlab code:


>> A = [1,2;2,4;3,6]

A =

     1     2
     2     4
     3     6

>> B = [1,2,3;4,5,6]

B =

     1     2     3
     4     5     6


 sum(A)

ans =

     6    12

 sum(sum(B))

ans =

    21

>> (A.*B').^20

ans =

   1.0e+31 *

    0.0000    0.0000
    0.0000    0.0000
    0.0000    1.3367


 (A*B).^20

ans =

   1.0e+33 *

    0.0000    0.0000    0.0000
    0.0000    0.0000    0.0003
    0.0000    0.0134    1.1594

10. Load data to Matlab

Using MATLAB, do the following procedure:

1. Download the Data file to your directory, name it as hw5.mat, and load it into your MATLAB session by: >> load hw5;

2. Check what variables (i.e., arrays) are defined in this data file by running: >> whos

3. Calculate the sum, mean and standard deviation of the data stored in the second and the third column.


whos
 Name        Size            Bytes  Class     Attributes
 hw5       360x7             20160  double


sum([hw5(:,2); hw5(:,3)])

ans =

  6.5315e+05


mean([hw5(:,2); hw5(:,3)])

ans =

 907.1584


std([hw5(:,2); hw5(:,3)])

ans =

 414.9253
Personal tools