[Numpy-discussion] Matlab/Numpy index order

brechmos craig@brechmos....
Tue May 12 14:51:21 CDT 2009

```I am very new to Numpy and relatively new to Python. I have used Matlab for
15+ years now.  But, I am starting to lean toward using Numpy for all my
work.

One thing that I am not understanding is the order of data when read in from
a file.  Let's say I have a 256x256x150 uint16 dataset (MRI, 150 slices).
In Matlab I would read it in as:

>> fp=fopen(<binary file>);
>> fclose(fp);
>> c = reshape(d, [256 256 150]);
>> imagesc(c(:,:,1));

I am very used to having it read it in and doing the reshaping such that it
is 256 rows by 256 columns by 150 slices.

Now, Numpy, I can read in the binary data using fromfile (or open, read,
close):

In [85]: a = fromfile(<binary file>, dtype='int16')
In [86]: b = array(struct.unpack('<%dH'%(256*256*150),
a)).reshape(150,256,256)

So, in Numpy I have to reshape it so the "slices" are in the first
dimension.  Obviously, I can do a b.transpose( (1,2,0) ) to get it to look
like Matlab, but...

I don't understand why the index ordering is different between Matlab and
Numpy.  (It isn't a C/Fortran ordering thing, I don' think).

Is the data access faster if I have b without the tranpose, or can I
transpose it so it "looks" like Matlab without taking a hit when I do
imshow( b[:,:,0] ).

Any help for a Numpy newbie would be appreciated.
--
View this message in context: http://www.nabble.com/Matlab-Numpy-index-order-tp23509178p23509178.html
Sent from the Numpy-discussion mailing list archive at Nabble.com.

```