I have a 2D array A
of shape (4,3)
, and a 1D array a
of shape (4,)
. I want to swap the first two rows of A
, as well as the first two elements in a
. I did the following:
A[0,:],A[1,:] = A[1,:],A[0,:]
a[0],a[1] = a[1],a[0]
Apparently, it works for a
, but fails for A
. Now, the second row becomes the first row, but the first row remains unchanged. If I do the following:
first_row_copy = A[0,:].copy()
A[0,:] = A[1,:]
A[1,:] = first_row_copy
Then, it seems to work. Why the first method doesn't work? (but works for a
) Also, what's the difference between A_copy = A[0,:].copy()
and A_copy = A[0,:]
?