Slices

This page is an exploration of a web page created by Jeff Fessler, University of Michigan. Julia Slices

Create an array of two digit numbers. The first number is the row. The second number is the column. There are three rows and four columns.


                A = [10*m+n for m=1:3, n=1:4]
                3x4 Matrix{Int64}:
                11  12  13  14
                21  22  23  24
                31  32  33  34
            

Slicing multiple rows or columns:


                A[1:2,:]
                2x4 Matrix{Int64}:
                11  12  13  14
                21  22  23  24

                A[:, 3:4]
                3x2 Matrix{Int64}:
                13  14
                23  24
                33  34
            

Beware! Slicing a single row gets a one dimensional vector not a 1 * 4 array.


                A[2,:]
                4-element Vector{Int64}:
                21
                22
                23
                24
            

A[[2],:] # this unusual version of slicing a single row returns a 1x4 array!


                A[[2],:]
                1x4 Matrix{Int64}:
                21  22  23  24
            

Note the type description "rxc Matrix" means r rows and c columns.

Suppose we take a row and a column:


                B = A[[1], 1:3]
                1x3 Matrix{Int64}:
                11  12  13

                C = A[1:3, [1]]
                3x1 Matrix{Int64}:
                11
                21
                31
            

Now B*C is the dot product but returned as a 1 * 1 Matrix. To get the dot product as a scalar we need to use the adjoint operator: '


                # row-vector (11, 12, 13) * column vector (11, 21, 31)
                # i.e a 1*3 matrix times a 3*1 matrix
                # 11*11 + 12*21 + 13*31 = 776
                B*C
                1x1 Matrix{Int64}:
                776
                
                # Whereas if we get the first three columns of the first row as a column vector
                # and the first column as a vector, then use the adjoint,
                # the dot product now comes out as a scalar value.
                D = A[1,1:3]' * A[:,1]
                776
            

As you would expect, if you multiply a column vector by a row vector, i.e. a 3x1 matrix times a 1x3 matrix, then you get the outer product:


                C * B = (11, 21, 31)' * (11, 12, 13)
                3x3 Matrix{Int64}:
                121  132  143
                231  252  273
                341  372  403