Alain
Alain

Reputation: 403

basic manipulation of a matrix in Python and row dimension

I read a matrix from a binary file using the following function:

def read_data(name,nx,ny):
    data = np.fromfile(name, count=nx*ny, dtype=np.float64).reshape((nx,ny), order='F')
    return data

So for instance:

A = read_data('myfile.xyz',10,1000)

In this example A is a 10 by 1000 matrix. If I look for the shape of a line of A, I get:

print A[0,:].shape
>>> (1000L,)

Now, let's say I multiply A by an 10 by 10 matrix B:

A = np.dot(B,A)

The dimension of A is still 10 by 1000 however, when looking at the shape of the first line of A I get:

print A[0,:].shape
>>> (1000L, 1L)

instead of

>>> (1000L,)

previously. Can anyone explain to me what is happening here ?

Upvotes: 0

Views: 96

Answers (1)

Rory Yorke
Rory Yorke

Reputation: 2236

My guess is your "B" is a matrix type, not a plain ndarray:

In [30]: a=np.random.randn(10,1000)

In [31]: b=np.random.randn(10,10)

In [32]: np.dot(b,a)[:,0].shape
Out[32]: (10,)

In [33]: np.dot(np.matrix(b),a)[:,0].shape
Out[33]: (10, 1)

Upvotes: 4

Related Questions