Reputation: 1627
I am trying to cluster pixels using k-means in an image (480 x 640), and so I am trying to initialise an empty numpy array, which represents a 1-column vector.
What I am trying to achieve is get all pixel values from the 2D array and add them in a 1D vertical vector, like this:
[ [ value ],
[ value ],
...
[ value ] ]
Upvotes: 0
Views: 350
Reputation: 103834
Suppose you have a matrix 640 x 480:
>>> a=np.arange(640*480).reshape(640,480)
>>> a
array([[ 0, 1, 2, ..., 477, 478, 479],
[ 480, 481, 482, ..., 957, 958, 959],
[ 960, 961, 962, ..., 1437, 1438, 1439],
...,
[305760, 305761, 305762, ..., 306237, 306238, 306239],
[306240, 306241, 306242, ..., 306717, 306718, 306719],
[306720, 306721, 306722, ..., 307197, 307198, 307199]])
You can assign a columnar value like so:
>>> a[:,479]=0
>>> a
array([[ 0, 1, 2, ..., 477, 478, 0],
[ 480, 481, 482, ..., 957, 958, 0],
[ 960, 961, 962, ..., 1437, 1438, 0],
...,
[305760, 305761, 305762, ..., 306237, 306238, 0],
[306240, 306241, 306242, ..., 306717, 306718, 0],
[306720, 306721, 306722, ..., 307197, 307198, 0]])
# ^^^ Note the 0 here
You can get the columnar value like so:
>>> a[:,478]
array([ 478, 958, 1438, 1918, ..., 306238, 306718, 307198])
Upvotes: 1
Reputation: 36
Is numpy.reshape()
what you want?
a = np.array([[1,2,3], [4,5,6]])
a.reshape(6,1)
array([[1],
[2],
[3],
[4],
[5],
[6]])
Upvotes: 2