In [17]:
import numpy as np

Matrix Indexing

Col Vec vs. Row Vec

Vectors are usually 1 dim.

To access an attribute/element of a vector as $\vec v_i$:

v[i]

When having a stacked vector in matrix, to access the $i$-th vector stacked as col vectors:

mat[:, i]

To access the $i$-th vector stacked as row vectors:

mat[i, :]
# or simply
mat[i]

Matrix Appending

Initialization

Initialize an empty matrix with given dim using reshape.


In [19]:
dim = 3
X = np.array([]).reshape(dim, 0)
X


Out[19]:
array([], shape=(3, 0), dtype=float64)

Append Vec

Append col-vec to matrix along col.


In [51]:
vec = [1, 2, 3]
Y = np.c_[X, vec]
Y


Out[51]:
array([[ 1.],
       [ 2.],
       [ 3.]])

In [52]:
Y = np.c_[Y, vec]
Y


Out[52]:
array([[ 1.,  1.],
       [ 2.,  2.],
       [ 3.,  3.]])

Append Mat

Append a matrix along col, differs in dim of axis=1

Convert vec to mat by expand_dims


In [53]:
mat = np.expand_dims(vec, axis=1)
mat


Out[53]:
array([[1],
       [2],
       [3]])

In [54]:
np.hstack((Y, mat))


Out[54]:
array([[ 1.,  1.,  1.],
       [ 2.,  2.,  2.],
       [ 3.,  3.,  3.]])

In [ ]: