3. Matrices

 

3.1 Creating matrices

3.2 Locating elements of matrices

3.3 Concatenating matrices

3.4 Determining the size of a matrix

3.5 Transposing a matrix

3.6 Creating matrices with short-hand methods

3.7 Checking the status of matrices

3.8 Clearing and emptying matrices

3.9 Practicing with matrices

 

top of page

3.1 Creating matrices

 

Code 3.1.1:

 

A = [1, 2, 3, 4, 5, 6]

 

Output  3.1.1:

 

A =

 

     1     2     3     4     5     6

 

 

Code 3.1.2:

 

B = [4, .8, -.12, 0, -24]

 

Output 3.1.2:

 

B =

 

    4.0000    0.8000   -0.1200         0  -24.0000

 

 

Code 3.1.3:

 

C = [4 .8 -.12 0 -24] 

 

Output  3.1.3:

 

C =

 

     4.0000    0.8000   -0.1200         0  -24.0000

 

 

 

Code 3.1.4:

 

D = [1 2; 3 4; 5 6]

 

Output 3.1.4:

 

D =

 

     1     2

     3     4

     5     6

 

Code 3.1.5:

 

D = [1 2; 3 4; 5 6];

 

Output 3.1.5:

 

>> 

 

Code 3.1.6:

 

E = [1 2 3; 4 5; 6 7 8];

 

Output 3.1.6:

 

??? Error using ==> vertcat

All rows in the bracketed expression must have the same

number of columns.

 

 

top of page

3.2 Locating elements of matrices

    

Code 3.2.1:

 

D(1,1)

 

Output 3.2.1:

 

ans =

 

     1

 

Code 3.2.2:

 

D(2,2)

 

Output 3.2.2:

 

ans =

 

     4

 

 

 

Code 3.2.2:

 

D(:,1)

 

Output 3.2.2:

 

ans =

 

     1

     3

     5

 

 

Code 3.2.3:

 

D(:,2)

 

Output 3.2.3:

 

ans =

 

     2

     4

     6

 

Code 3.2.4:

 

D(1,:)

 

Output 3.2.4:

 

ans =

 

     1     2

 

 

Code 3.2.5:

 

E = [1 2 3 4; 5 6 7 8; 9 10 11 12]

 

Output 3.2.5:

 

E =

 

     1     2     3     4

     5     6     7     8

     9    10    11    12

 

 

Code 3.2.6:

 

E(end,2)

 

Output 3.2.6:

 

ans =

 

    10

 

 

Code 3.2.7:

 

E(2,end)

 

Output 3.2.7:

 

ans =

 

     8

 

Code 3.2.8:

 

E(2,end-1)

 

Output 3.2.8:

 

ans =

 

     7

 

 

top of page

3.3 Concatenating matrices

    

Code 3.3.1:

 

F = [10 11 12];

G = [13 14 15];

H = [F;G]

 

Output 3.3.1:

 

H =

 

    10    11    12

    13    14    15

 

 

Code 3.3.2:

 

H = [F G]

 

Output 3.3.2:

 

H =

 

    10    11    12    13    14    15

 

I = [20 21 22 23 24 25 26];

 

 

Code 3.3.3:

 

J = [H;I]

 

Output 3.3.3:

 

??? Error using ==> vertcat

All rows in the bracketed expression must have the same

number of columns.

 

Code 3.3.4:

 

K  = [H I]

 

Output 3.3.4:

 

K =

 

10  11  12  13  14  15  20  21  22  23  24  25  26

 

 

top of page

3.4 Determining the size of a matrix

    

Code  3.4.1:

 

size(I)

 

Output 3.4.1:

 

ans =

 

     1     7

 

 

Code 3.4.2:

 

sz_K = size(K)

 

Output 3.4.2:

 

sz_K =

 

     1     17

 

 

Code 3.4.3:

 

[rows columns] = size(K)

 

Output 3.4.3:

 

rows =

 

     1

 

 

columns =

 

    17

 

Code  3.4.4:

 

JJ = [1:4;5:8]

 

Output 3.4.4:

 

JJ =

 

     1     2     3     4

     5     6     7     8

 

Code  3.4.5:

 

size (JJ)

 

Output 3.4.5:

 

ans =

 

     2     4

 

Code 3.4.6:

 

length(JJ)

 

 

 

 

Output 3.4.6:

 

ans =

 

     4

 

Code 3.4.7:

 

KK = [1 5; 2 6; 3 7; 4 8]

 

Output 3.4.7:

 

ans =

 

     1     5

     2     6

     3     7

     4     8

 

Code 3.4.8:

 

length(KK)

 

Output 3.4.8:

 

ans =

 

     4

 

 

top of page

3.5 Transposing a matrix

 

 

Code 3.5.1:

 

J = [1 2 3 4];

K = [5;6;7;8]

 

Output  3.5.1:

 

K =

 

     5

     6

     7

     8

 

Code  3.5.2:

 

L = [J;K]

 

Output  3.5.2:

 

??? Error using ==> vertcat

All rows in the bracketed expression must have the same

number of columns.

 

 

Code  3.5.3:

 

K’

 

Output  3.5.3:

 

ans =

 

     5     6     7     8

 

 

Code  3.5.4:

 

L = [J;K']

 

Output 3.5.4:

 

L =

 

     1     2     3     4

     5     6     7     8

 

 

Code 3.5.4:

 

L'

 

Output 3.5.4:

 

ans =

 

     1     5

     2     6

     3     7

     4     8

 

 

 

top of page

3.6 Creating matrices with shorthand methods

 

Code 3.6.1:

 

M = [1 2 3 4 5 6];

 

Code 3.6.2:

 

M = [1:6]

 

Output 3.6.2:

 

M =

 

     1     2     3     4     5     6

 

 

 

Code 3.6.3:

 

MM = [1:.5:6]

 

Output 3.6.3:

 

MM =

 

    1.0000    1.5000    2.0000    2.5000    3.0000    3.5000    4.0000    4.5000    5.0000    5.5000    6.0000

 

 

 

Code 3.6.4:

 

MM'

 

Output 3.6.4:

 

ans =

 

    1.0000

    1.5000

    2.0000

    2.5000

    3.0000

    3.5000

    4.0000

    4.5000

    5.0000

    5.5000

    6.0000

 

 

Code 3.6.5:

 

Descending_Matrix = [5:-2:-7]

 

Output 3.6.5:

 

Descending_Matrix =

 

     5     3     1    -1    -3    -5    -7

 

 

Code 3.6.6:

 

s = [5:-6:-3]

 

Output 3.6.6:

 

s =

 

     5    -1

 

 

Code 3.6.7:

 

s = linspace(5,-3,10);

s’

 

Output 3.6.7:

 

ans =

 

    5.0000

    4.1111

    3.2222

    2.3333

    1.4444

    0.5556

   -0.3333

   -1.2222

   -2.1111

   -3.0000

 

Code 3.6.8:

 

help logspace

 

Output 3.6.8:

 

 LOGSPACE Logarithmically spaced vector.

    LOGSPACE(X1, X2) generates a row vector of 50 logarithmically

    equally spaced points between decades 10^X1 and 10^X2.  If X2

    is pi, then the points are between 10^X1 and pi.

 

    LOGSPACE(X1, X2, N) generates N points.

    For N < 2, LOGSPACE returns 10^X2.

 

    See also LINSPACE, :.

 

Code 3.6.9:

 

sss = logspace(1,2,5)

 

Output 3.6.9:

 

sss =

 

   10.0000   17.7828   31.6228   56.2341  100.0000

 

               

top of page

3.7 Checking the status of matrices

 

 

Code 3.7.1:

 

who

 

Output  3.7.1:

 

Your variables are:

 

Descending_Matrix  JJ                 lg                 sss               

II                 ans                s                 

 

 

 

Code 3.7.2:

 

whos

 

Output 3.7.2:

 

  Name                    Size         Bytes  Class

 

  Descending_Matrix       1x7          56     double array

  II                      2x4          64     double array

  JJ                      4x2          64     double array

  ans                     1x2          16     double array

  lg                      1x5          40     double array

  s                       1x10         80     double array

  sss                     1x5          40     double array

 

Grand total is 45 elements using 360 bytes

 

 

top of page

3.8 Clearing and emptying matrices

 

 

Code 3.8.1:

 

clear s

whos

 

Output 3.8.1:

 

  Name                    Size         Bytes  Class

 

  Descending_Matrix       1x7          56     double array

  II                      2x4          64     double array

  JJ                      4x2          64     double array

  ans                     1x2          16     double array

  lg                      1x5          40     double array

  sss                     1x5          40     double array

 

Grand total is 35 elements using 280 bytes

 

 

Code 3.8.2:

 

sss

size(sss)

sss(end-1:end) = []

size(sss)

 

Output 3.8.2:

 

sss =

 

   10.0000   17.7828   31.6228   56.2341  100.0000

 

 

   ans =

 

     1     5

 

 

ans =

 

   10.0000   17.7828   31.6228

 

ans =

 

     1     3

 

Code 3.8.3:

 

sss = []

size(sss)

 

Output 3.8.3:

 

sss =

 

     []

 

 

ans =

 

     0     0

 

 

 

Code 3.8.4:

 

matrix_to_be_added_to = []

matrix_to_be_added_to =[matrix_to_be_added_to; 1]

matrix_to_be_added_to =[matrix_to_be_added_to; 2]

matrix_to_be_added_to =[matrix_to_be_added_to; 3]

matrix_to_be_added_to =[matrix_to_be_added_to; 4]

 

Output 3.8.4:

 

matrix_to_be_added_to =

 

     []

 

matrix_to_be_added_to =

 

     1

 

matrix_to_be_added_to =

 

     1

     2

 

matrix_to_be_added_to =

 

     1

     2

     3

 

matrix_to_be_added_to =

 

     1

     2

     3

     4