Hi Hernán,

I often do this type of thing by using index ranges. For instance:

perldl> $b = pdl [0..10]
perldl> p $a = $b(1:-1) - $b(0:-2)
[1 1 1 1 1 1 1 1 1 1]

If you try this in a script, make sure to use PDL::NiceSlice;

In this example $b(1:-1) consist of the second through last elements of $b,
and $b(0:-2) consists of the first through second to last elements of
$b.

So subtracting them is equivalent to 

a(i) = b(i+1) - b($i)

Note that in your definition a(0) would be undefined since you refer
to b(i-1). But you could do this:

perldl> $b = pdl [0..10]
perldl> $a = zeroes(11)
perldl> $a(1:-1) .= $b(0:-2) - $b(1:-1)
perldl> p $a
[0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1]

which is equivalent to 

a(0) = 0
a(i) = b(i-1) - b(i+1) for i>0

I wrote this in a hurry, so please write back if any of this is
unclear. Welcome to the wonderful world of PDL!

-roban

On 17:28, Mon 18 Jun 07, Hernán De Angelis wrote:
> Hi everyone,
> 
> I am a new PDL user and this is my first post to this list. I am
> delighted with the power and flexibility of PDL and would like to use
> it more often in my everyday work and modelling. I am now writing in
> the hope that someone can give some advice on the following problem:
> 
> I have some m x 1 piddles representing values of different parameters
> along a profile. As everyone in this list knows, PDL is very good at
> doing maths in a one-to-one fashion, i.e. between cells with the same
> index value. However, this time I need to calculate values using
> different indexes, i.e.:
> 
> a(i) = [ b(i-1) - b(i+1) ] / dx
> 
> I would normally do this by operating inside a loop using standard
> Perl arrays, but this time it will more convenient for me to use PDL
> piddles and I therefore wonder if it would be possible to use a
> built-in function that will allow me to do such calculations without
> invoking loops.
> 
> I have already looked at the documentation (PDL::Indexing but I did
> not manage to figure out a solution, so any hint will be appreciated.
> 
> Thanks
> 
> Hernán
> 
> 
> -- 
> 
> Hernán De Angelis
> Linux user # 397217
> 
> _______________________________________________
> Perldl mailing list
> [email protected]
> http://mailman.jach.hawaii.edu/mailman/listinfo/perldl
> 

_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to