[Numpy-discussion] Reshaing continued

Karthikesh Raju karthik at james.hut.fi
Sun Sep 5 23:28:03 CDT 2004


On Sun, 5 Sep 2004, Stephen Walton wrote:

> On Sun, 2004-09-05 at 02:44, Karthikesh Raju wrote:
>
> > example a = reshape(arange(0,18),(2,3,3))
> >
> > a[0,:,:], a[1,:,:] should be rows wise extracts like
> >
> > a[0,:,:] = 0 3 6
> >            1 4 7
> >            2 5 8
> >
>
> I'm not certain why you expect the transpose of the actual result here.
> There are two possibilities. MATLAB arrays are column major (first index
> varies most rapidly), so in MATLAB (one-based indexing):
>


The transpose was another person's reply to the above question. Actually,
the reason i was doing all this was because i was working on "a
dataloader" that allowed me to dump and load variables in a ascii text
file, similar to what Matlab's mat format does. This worked fine as long
as the array dimension was 2. Now i need 3D array support, and one idea
was to convert all the dimensions into a tuple, load the data and reshape
as per my the array dimension requirement.

Obviously, both being different (column major vs row major), i elements
would be wrong. Hence i wanted to see if reshape could have a flag, that
told it to do either row wise or column wise reshaping.

A partial support for 3D has been by extending the number of columns in a
2D matrix, so each new dimension is a block matrix in the columns. This
works fine, but again another day when i need 4D it would break. This is
why i thought i could play with reshape to get things correct once and for
all.

Warm regards

karthik


> >> A=reshape([0:17],[2,3,3]);
> >> M=reshape(A(1,:,:),[3,3])
> M =
>
>      0     6    12
>      2     8    14
>      4    10    16
>
> This is the same thing you would get in MATLAB from
> M=reshape([0,2,4,6,8,10,12,14,16],[3,3])
>
> numarray arrays are row major (last index varies most rapidly), so in
> numarray:
>
> >>> A=reshape(arange(0,18), (2,3,3))
> >>> M=A[0,:,:]
> >>> M
> array([[0, 1, 2],
>        [3, 4, 5],
>        [6, 7, 8]])
>
> This is the same thing you get for M=reshape(arange(0,9),(3,3)).
>
> --
> Stephen Walton <stephen.walton at csun.edu>
> Dept. of Physics & Astronomy, Cal State Northridge
>




More information about the Numpy-discussion mailing list