numpy.array
numpy.matrix
Notes
ndims(a)
ndim(a) or a.ndim
get the number of dimensions of a (tensor rank)
numel(a)
size(a) or a.size
get the number of elements of an array
size(a)
shape(a) or a.shape
get the “size” of the matrix
size(a,n)
a.shape[n-1]
get the number of elements of the nth dimension of array a. (Note that MATLAB® uses 1 based indexing while Python uses 0 based indexing,
[ 1 2 3; 4 5 6 ]
array([[1.,2.,3.],
[4.,5.,6.]])
mat([[1.,2.,3.],
[4.,5.,6.]]) or
mat(“1 2 3; 4 5 6”)
2×3 matrix literal
[ a b; c d ]
vstack([hstack([a,b]),
hstack([c,d])])
bmat(‘a b; c d’)
construct a matrix from blocks a,b,c, and d
a(end)
a[-1]
a[:,-1][0,0]
access last element in the 1xn matrix a
a(2,5)
a[1,4]
access element in second row, fifth column
a(2,:)
a[1] or a[1,:]
entire second row of a
a(1:5,:)
a[0:5] or a[:5] or a[0:5,:]
the first five rows of a
a(end-4:end,:)
a[-5:]
the last five rows of a
a(1:3,5:9)
a[0:3][:,4:9]
rows one to three and columns five to nine of a. This gives read-only access.
a([2,4,5],[1,3])
a[ix_([1,3,4],[0,2])]
rows 2,4 and 5 and columns 1 and 3. This allows the matrix to be modified, and doesn’t require a regular slice.
a(3:2:21,:)
a[ 2:21:2,:]
every other row of a, starting with the third and going to the twenty-first
a(1:2:end,:)
a[ ::2,:]
ever