0

Why is it that:

a = [1, 2 ,3]
b = [3, 4, 5]
t= np.vstack([a,b])
print(t)        # 1D array of 2 elements each of size 3
print(t[0])     # first elem of 1D array
print(t[0:])    # first row of 1D array
print(t[1:])    # ?
u = np.array([[6, 7 ,8], [9, 10, 11]])
print(u)        # 1D array of 2 elements each of size 3
print(u[0])     # first elem of 1D array
print(u[0:])    # first row of 1D array
print(u[1:])    # ?

prints in last line entire array instead of only first row?

[[1 2 3]
 [3 4 5]]  
[1 2 3]    
[[1 2 3]
 [3 4 5]]
[[3 4 5]]
[[ 6  7  8]
 [ 9 10 11]]
[6 7 8]
[[ 6  7  8]
 [ 9 10 11]]
[[ 9 10 11]]

Why the t[1:] in each case gives the second elem. of 1D array as it should give the second row of multidim array?

mCs
  • 2,591
  • 6
  • 39
  • 66
  • You may refer to vstack documentation https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.vstack.html , vstack basically creates an array of given array vertically stacked(row wise). t[0] will have first array, t[1] with second and so on.. – Vivek Harikrishnan Nov 25 '17 at 09:32
  • This question has been inspired by a lack of basic understanding of python's slice notation, rather than a misunderstanding of vstack. – cs95 Nov 25 '17 at 09:37
  • @VivekHarikrishnan @COLDSPEED I have added comments according to your answer. 1) Are they ok? 2) Why the `t[1:]` in each case gives the second elem. of 1D array as it should give the second row of multidim array? – mCs Nov 25 '17 at 10:22
  • You are confusing `t[0:]` with `t[0,:]`. A comma matters when selecting rows versus columns. – hpaulj Nov 25 '17 at 12:22

0 Answers0