There are multiple questions on StackOverflow, asking how the comma syntax works, but most of them refer to m[:,n]
which refers to the nth
column. Similarly, m[n,:]
refers to the nth
row. I find this method of slicing used in the labs of Machine Learning Specialization by Andrew Ng. But does this slicing have any advantage over m[n]
?
For an array with 2 or more dimensions, m[n]
and m[n, :]
are identical. The first can be considered shorthand for the second.
For an array with 1 dimension, m[n]
will return element n
, and m[n, :]
will result in an error.
I personally would choose m[n, :]
in some cases to make the code more human-readable: for example, when you know that m
is two-dimensional, then m[n, :]
immediately implies this to the reader, whereas m[n]
might leave them having to guess at whether m
is 1D or 2D.