Re: Another simple one [message #55061] |
Mon, 30 July 2007 09:19  |
Jean H.
Messages: 472 Registered: July 2006
|
Senior Member |
|
|
> Is that possible? Or does my array have to be square so have to
> truncate the whole thing at element 4 for example?
>
> Cheers,
>
> Snudge42
>
Hi,
You can also use a 1D array containing all of your data... then you
should know which entries correspond to which line...
ex: a = [1,2,3,4,5,6]
you can think of A as
1,2,3
,4,5
, ,6
So, but this starts to be useful with big arrays, you can have a 2D
array that indexes the 1D array...
ex:
indices2Dto1D = [[0,1,2],[-1,3,4],[-1,-1,5]]
print, "value of 2,2 = ", a[indices2Dto1D[2,2]] ==> 6
and
indices1Dto2D = [0,1,2,4,5,8]
print, "2D coords of the value 6 = ", indices1Dto2D[where(a eq 6)] ==>
8 (this is the 1D coordinate in the 2D array... you can transform it
back to 2,2)
Jean
PS: I use this all the time to keep satellite images covering a study
area having a crazy shape... I save about 75% of the otherwise required
memory! ... I have to keep only 1 array covering the entire area, and
all the other arrays cover only the study area!
|
|
|
Re: Another simple one [message #55073 is a reply to message #55061] |
Sun, 29 July 2007 12:39   |
MarioIncandenza
Messages: 231 Registered: February 2005
|
Senior Member |
|
|
On Jul 28, 12:58 am, snudge42 <snudg...@gmail.com> wrote:
> How do I truncate a multidimensional array at a different place for
> each dimension i.e. I start with 3x6 array of values A=
>
> 0 0 0
> 0 0 0
> 0 0 0
> 0 0 0
> 0 0 0
> 0 0 0
>
> and want to truncate each dimension at a fixed value stored in an
> array B, where the values are (2,3,4) for example so that I get:
>
> 0 0 0
> 0 0 0
> 0 0 0
> 0 0
> 0
>
By 'truncate', you mean either "perform calculations on only part of
array A" or "write output of only part of array A". For the second
case, the I/O penalty is far greater than the for-loop penalty, just
use a loop. For the first case, consider this:
;NOTE: this example truncates along rows, you'll need to TRANSPOSE to
do columns
btrunc=a*0; initialize truncation helper array
for i=0l,n_elements(b)-1 do btrunc[0]=(lindgen(b[i]+1))+1 gt 0; anyone
care to try getting rid of this loop?
print,trunc
> 1 1 1 0 0
> 1 1 1 1 0
> 1 1 1 1 1
; from here, you can manipulate A in several ways:
; such as setting truncated components to NaN:
ftrunc=where(trunc eq 0)
anew=a & anew[ftrunc]=!values.NaN
; setting truncated components to 0:
anew= a * btrunc;
Hope this helps,
--Edward H.
|
|
|
|
|
|
Re: Another simple one [message #55260 is a reply to message #55061] |
Wed, 08 August 2007 21:43  |
snudge42
Messages: 4 Registered: July 2007
|
Junior Member |
|
|
On Jul 31, 2:19 am, "Jean H." <jghas...@DELTHIS.ucalgary.ANDTHIS.ca>
wrote:
>> Is that possible? Or does my array have to be square so have to
>> truncate the whole thing at element 4 for example?
>
>> Cheers,
>
>> Snudge42
>
> Hi,
>
> You can also use a 1D array containing all of your data... then you
> should know which entries correspond to which line...
>
> ex: a = [1,2,3,4,5,6]
>
> you can think of A as
> 1,2,3
> ,4,5
> , ,6
>
> So, but this starts to be useful with big arrays, you can have a 2D
> array that indexes the 1D array...
>
> ex:
> indices2Dto1D = [[0,1,2],[-1,3,4],[-1,-1,5]]
> print, "value of 2,2 = ", a[indices2Dto1D[2,2]] ==> 6
> and
> indices1Dto2D = [0,1,2,4,5,8]
> print, "2D coords of the value 6 = ", indices1Dto2D[where(a eq 6)] ==>
> 8 (this is the 1D coordinate in the 2D array... you can transform it
> back to 2,2)
>
> Jean
> PS: I use this all the time to keep satellite images covering a study
> area having a crazy shape... I save about 75% of the otherwise required
> memory! ... I have to keep only 1 array covering the entire area, and
> all the other arrays cover only the study area!
Lots to think about there, thanks everyone. =)
|
|
|