Re: Re-ordering columns or rows of an array efficiently
- From: glen herrmannsfeldt <gah@xxxxxxxxxxxxxxxx>
- Date: Wed, 28 Nov 2007 14:16:36 -0800
Mike wrote:
I need to re-order the columns of an array. The way that I've done
this is to construct a vector of integers E which contains the desired
order of the columns, and then I do this:
A0=A(:,E)
A=A0
Of course, this requires having a matrix the size of A allocated
twice. Since A is several gigabytes on my system, this is a rather
undesirable way to do things. Yet, I get errors if I try something
like A=A(:,E). Can anyone suggest a way of doing this that doesn't
require as much memory?
You say reorder, such that the elements of E are unique. In general
they are not unique, so a full temporary array is the solution.
A solution similar to the "inverting a permutation" problem
previously discussed here, with a one column temporary and a
one row of LOGICAL array:
Set the LOGICAL array to .FALSE..
Copy the first column to the temporary column. Copy the E(1)
column to column 1 and set the first element of the logical array
to .TRUE.. Continue copying columns and setting array elements
to .TRUE. until you reach the one where E(i)=1, in which case
you copy the temporary column back in. If all elements of the
LOGICAL array are .TRUE. you are done, otherwise pick one that
is .FALSE. and repeat the column copying process.
It isn't really necessary to use a whole column temporary but it
is easier to describe, and probably faster.
You could do the rearrangement one row at a time, which requires
only one temporary row.
DO I=1,N
A(I,:)=A(I,E)
ENDDO
This may or may not be faster than the column copying method.
The relative speed might depend on the row and column dimensions.
-- glen
.
- Follow-Ups:
- Re: Re-ordering columns or rows of an array efficiently
- From: Dan Nagle
- Re: Re-ordering columns or rows of an array efficiently
- References:
- Prev by Date: Re: Re-ordering columns or rows of an array efficiently
- Next by Date: Re: Re-ordering columns or rows of an array efficiently
- Previous by thread: Re: Re-ordering columns or rows of an array efficiently
- Next by thread: Re: Re-ordering columns or rows of an array efficiently
- Index(es):