MATLAB Examples

Section 7 - Performing Calculations

You can create a PDF or HTML version of this MATLAB file by choosing opening it in the MATLAB Editor and choosing "File > Publish ..."

Contents

Creating Matrices in MATLAB

my_scalar = 3.1415
my_scalar =

    3.1415

my_vector1 = [1, 5, 7]
my_vector1 =

     1     5     7

my_vector2 = [1; 5; 7]
my_vector2 =

     1
     5
     7

my_matrix = [8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]
my_matrix =

     8    12    19
     7     3     2
    12     4    23
     8     1     1

row_vector1 = [1 2 3]
row_vector1 =

     1     2     3

row_vector2 = [3 2 1]
row_vector2 =

     3     2     1

matrix_from_row_vec = [row_vector1 ; row_vector2]
matrix_from_row_vec =

     1     2     3
     3     2     1

column_vector1 = [1;3]
column_vector1 =

     1
     3

column_vector2 = [2;8]
column_vector2 =

     2
     8

matrix_from_col_vec = [column_vector1 column_vector2]
matrix_from_col_vec =

     1     2
     3     8

my_matrix = [8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]
my_matrix =

     8    12    19
     7     3     2
    12     4    23
     8     1     1

combined_matrix = [my_matrix, my_matrix]
combined_matrix =

     8    12    19     8    12    19
     7     3     2     7     3     2
    12     4    23    12     4    23
     8     1     1     8     1     1

time = 0:100:2300
time =

  Columns 1 through 10

           0         100         200         300         400         500         600         700         800         900

  Columns 11 through 20

        1000        1100        1200        1300        1400        1500        1600        1700        1800        1900

  Columns 21 through 24

        2000        2100        2200        2300

time_dec = 2300:-100:0
time_dec =

  Columns 1 through 10

        2300        2200        2100        2000        1900        1800        1700        1600        1500        1400

  Columns 11 through 20

        1300        1200        1100        1000         900         800         700         600         500         400

  Columns 21 through 24

         300         200         100           0

Indexing Into a Matrix

my_vector1 = [1 5 7]
my_vector1 =

     1     5     7

my_vector2 = [1; 5; 7]
my_vector2 =

     1
     5
     7

my_vector1(1)
ans =

     1

my_vector2(2)
ans =

     5

my_vector1(3)
ans =

     7

my_vector2(1)
ans =

     1

my_vector2(2)
ans =

     5

my_vector2(3)
ans =

     7

my_matrix = [8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]
my_matrix =

     8    12    19
     7     3     2
    12     4    23
     8     1     1

my_matrix(3,2)
ans =

     4

mat = [1  8 9 4 5 2 4 8
       9  3 7 4 8 2 8 8
       1  2 9 4 6 2 3 4
      10  2 2 4 6 7 3 2
       3 12 3 3 2 8 4 9]
mat =

     1     8     9     4     5     2     4     8
     9     3     7     4     8     2     8     8
     1     2     9     4     6     2     3     4
    10     2     2     4     6     7     3     2
     3    12     3     3     2     8     4     9

new_mat = mat(2:4,4:7)
new_mat =

     4     8     2     8
     4     6     2     3
     4     6     7     3

new_mat
new_mat =

     4     8     2     8
     4     6     2     3
     4     6     7     3

new_mat(2,3) = 1999
new_mat =

           4           8           2           8
           4           6        1999           3
           4           6           7           3