In MatLab if I want the first column of a matrix I simply do:

- Code: Select all
`first = b(:,1);`

which will yield a new matrix with dimensions of [r,c]. However, if I try something similar in Python:

- Code: Select all
`from numpy import *`

first = b[:,0]

I get a new array with dimensions [r,], a one-dimensional array. This is ok, until I try to join this new array with another of same length:

- Code: Select all
`new = hstack( (b[:,0], b[:,100:106]) )`

This elicits a very angry response from Python regarding a dimension mis-match. This can be overcome, in a somewhat ugly fashion by:

- Code: Select all
`new = hstack( (b[:,0:1], b[:,100:106]) )`

Whereas in MatLab this is a very simple operation:

- Code: Select all
`new = [b(:,1),b(:,100:105)]`

Question 1: Is there a more elegant way of stacking the arrays in Python? I doubt it, and this is not a big deal since I can get around this minor inconvenience.

Now, when I continue on with my array work, I am hitting another issue with dimensions, this time using mean(). If I take my array and want to perform a mean over the columns (i.e. axis = 1):

- Code: Select all
`new = mean(old,axis=1)`

I get, wait for it, a 1-d array! Again, I run into the same problem as above when trying to stack this "new" array with another array. I can force the array into 2-d via:

- Code: Select all
`new = atleast_2d( mean(old,axis=1) ) # Transposes (not really a transpose since 1-d) from shape of [r,] to [1,r]`

new = new.T # Fixes transpose issue

Again, this can be handled, but is a non-elegant way. I can't understand WHY numpy would default everything to 1-d as this is super annoying.

Question 2: Is there any way to preserve the two dimensions of my array after applying the mean, without invoking atleast_2d and .T?