
Hi All, yes, Reshaping can be done row wise using transpose, for example a = transpose(reshape(arange(0,9),(3,3))) but what about higher dimension arrays. 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 etc Why cant we define the axis along which reshape should work .. warm regards karthik ----------------------------------------------------------------------- Karthikesh Raju, email: karthik@james.hut.fi karthikesh.raju@gmail.com Researcher, http://www.cis.hut.fi/karthik Helsinki University of Technology, Tel: +358-9-451 5389 Laboratory of Comp. & Info. Sc., Fax: +358-9-451 3277 Department of Computer Sc., P.O Box 5400, FIN 02015 HUT, Espoo, FINLAND -----------------------------------------------------------------------

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):
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@csun.edu> Dept. of Physics & Astronomy, Cal State Northridge

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@csun.edu> Dept. of Physics & Astronomy, Cal State Northridge

On Sun, 2004-09-05 at 23:26, Karthikesh Raju wrote:
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.
I thought as much. This is an issue the, ahem, older folks on this list have struggled with since we began migrating some of our code from Fortran to C/C++. Fortran and C arrays are column and row major, respectively. I think your best solution is to use a well specified, language independent format for data storage and use the corresponding utilities to read and write it. This should solve your problem. For astronomical images, my community uses FITS, which carefully specifies the order in which the values are to be written to disk. I also learned at SciPy that HDF and CDF are becoming more widely used. According to my notes, PyTables should be able to read and write HDF5 files; see http://pytables.sourceforge.net. Perhaps this can help. Stephen Walton <stephen.walton@csun.edu> Dept. of Physics & Astronomy, CSU Northridge

Karthikesh Raju wrote:
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.
I am answering from rusty Numeric knowledge, not knowing whether the numarray implementation is different. I think you are mixing up how reshape and how transpose work. "reshape" doesn't actually touch the data; it only changes the size of the different dimensions. Therefore, the order of the data in the array can not be changed by reshape, nor can the number of data points. The transpose method also doesn't touch the actual data, but it changes the strides in which the data are used. transpose can change the order of N-dimensions, not only 2! Both operations are basically O(1), which practically means that they are instantaneous, no matter how large the arrays. After a transpose, the array is normally non-contiguous, which might mean that repeated walk-throughs are significantly slower, and it may pay to make a contiguous copy first. Rob -- Rob W.W. Hooft || rob@hooft.net || http://www.hooft.net/people/rob/

On Sun, 12 Sep 2004, Rob Hooft wrote:
After a transpose, the array is normally non-contiguous, which might mean that repeated walk-throughs are significantly slower, and it may pay to make a contiguous copy first.
For many operations on noncontiguous arrays, Numeric makes a contiguous temporary copy before performing the operation, so if you're going to do more than one thing with a noncontiguous array, you should make a copy yourself. Don't know if numarray works the same way, but I'd guess that it would. Warren Focke
participants (4)
-
Karthikesh Raju
-
Rob Hooft
-
Stephen Walton
-
Warren Focke