I have an x dimensional matrix in Numpy. For this example, I will use a 2x2 array.
np.array([[2, 2], [3,3]])
How would I alternate adding a row and column of some value so the result would look like:
array([[2, x, 3, x],
[x, x, x, x].
[2, x, 3, x],
[x, x, x, x]])
This answer gives a helpful start by saying to set rows in a correctly-sized destination matrix b from a matrix a like so a[::2] = b but what does the ::2 do in the slicing syntax and how can I make it work on columns?
In short what do the x y and z parameters do in the following: a[x:y:z]?
x,y,zin your example do. In short,zis a "step" parameterrangeandarange, and list slicing, the parameters arestart,stop,step.::2isslice(None,None,2), means every other one. Try this:np.arange(0,4,2)arangeorrandom).1::2still gives every other one, starting with 1, i.e. all odds. Row and column indexing are separated by a comma.