[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Generally accessing the rest of the elements in an array



tbowers wrote:
...
> a = indgen(3,2,4)
> 
> I want the equivalent of (in this case):
> b = (a[0,*,*])^2 + (a[1,*,*])^2 + (a[2,*,*])^2
> 
> but this requires *'ing the correct dimensions ([0,*,*] for 3 dims,
> [0,*,*,*] for 4 dims
> etc). What I need is a general way to access the "rest" of the data, as Paul
> Harvey
> would say.
> 
> Actually, what I *really* want is to access it all generally so if a is 3
> columns, it'll be
> as above
> 
> b = (a[0,*,*])^2 + (a[1,*,*])^2 + (a[2,*,*])^2
> but if it's 4 columns, it'll be
> b = (a[0,*,*])^2 + (a[1,*,*])^2 + (a[2,*,*])^2 + (a[3,*,*])^2
> 5 columns...
> b = (a[0,*,*])^2 + (a[1,*,*])^2 + (a[2,*,*])^2 + (a[3,*,*])^2 + (a[4,*,*])^2
> n columns...
> b = (a[0,*,*])^2 + (a[1,*,*])^2 + (a[2,*,*])^2 + ... + (a[n-1,*,*])^2
> 
> but I don't think this is possible without a for loop.
> 

Hi Todd,

I would guess that this is only a solution to your simplified example,
and not to your real problem:

b = TOTAL(a^2, 1)

Maybe it comes in useful somehow. It can be made more general by
combination with REFORM and TRANSPOSE, in order to sum over almost any
periodic subset of an array.

groetjes,
  Jaco van Gorkom


Oops! Sorry, I meant to stay in lurking for 3 months... now I still
can't make it into David's thread! Hello anyway, I'm Jaco, no dog.