I only briefly touched on "where". The "where" function is pretty awesome
compared with Matlab because you can actually save the "slice" in a
variable and manipulate it later. In Matlab, as I recall, you can only
manipulate a slice on the line in which the slice is created. In PDL, you
can store the slice in a variable and manipulate it later.

$a = sequence(20);
$b = $a->where($a % 2 == 0);
# Make all even values in $a odd:
$b += 1;


On Thu, Jan 24, 2013 at 10:40 AM, David Mertens <[email protected]>wrote:

> Just to clarify, here's something out of the pdl shell:
>
> # make 20 values from -9 to 10
> pdl> $a = zeroes(20)->xlinvals(-9, 10)
> pdl> p $a
> [-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10]
>
> # get a mask of true/false
> pdl> p $a == 4
> [0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0]
>
> # Get that offset:
> pdl> p which($a == 4)
> [13]
>
> # Mask all values that are even:
> pdl> p $a % 2 == 0
> [0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1]
>
> # Get those indices
> pdl> p which($a % 2 == 0)
> [1 3 5 7 9 11 13 15 17 19]
>
> # Flip the sign of even values
> pdl> $a->where($a % 2 == 0) *= -1
> pdl> p $a
> [-9 8 -7 6 -5 4 -3 2 -1 0 1 -2 3 -4 5 -6 7 -8 9 -10]
>
> # learn about approx:
> pdl> ? approx
> # use it for floating point numbers
> pdl> $a = sequence(20)->sqrt
> pdl> p $a
> [0 1  1.4142136  1.7320508 2   2.236068  2.4494897  2.6457513  2.8284271
> 3  3.1622777  3.3166248  3.4641016  3.6055513  3.7416574  3.8729833 4
> 4.1231056  4.2426407  4.3588989]
>
> # Find the square root of 2, approximately:
> pdl> p $a->approx(1.414)
> [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
>
> # The default epsilon, 1e-6, is too strict.
> # Find where $a is approximately 1.414, within 1e-3
> pdl> p $a->approx(1.414, 1e-3)
> [0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
>
> # Later calls to approx use same "epsilon" that we just set:
> pdl> p $a->approx(1.414)
> [0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
>
> # Get the index thereof
> pdl> p which($a->approx(1.414))
> [2]
>
> Hope that helps!
>
>
> On Thu, Jan 24, 2013 at 10:23 AM, Lee Goddard <[email protected]> wrote:
>
>> Thanks.
>>
>>
>> On 24/01/2013 15:50, Chris Marshall wrote:
>>
>>> which or whichND
>>>
>>> On Thu, Jan 24, 2013 at 9:46 AM, Lee Goddard <[email protected]> wrote:
>>>
>>>> Is there a PDL equivalant to MATLAB's 'find(x)' function, or will I
>>>> need to
>>>> write my own?
>>>>
>>>> TIA
>>>> Lee
>>>>
>>>> ind = find(X)
>>>> locates all nonzero elements of array X, and returns the linear
>>>> indicies of
>>>> those elements in vector ind. If X is a row vector, then ind is a row
>>>> vector; otherwise, ind is a column vector. If X contains no nonzero
>>>> elements
>>>> or is an empty array, then ind is an empty array.
>>>>
>>>> -- 
>>>> http://www.mathworks.com/help/**matlab/ref/find.html<http://www.mathworks.com/help/matlab/ref/find.html>
>>>>
>>>>
>>>> ______________________________**_________________
>>>> Perldl mailing list
>>>> [email protected]
>>>> http://mailman.jach.hawaii.**edu/mailman/listinfo/perldl<http://mailman.jach.hawaii.edu/mailman/listinfo/perldl>
>>>>
>>>
>>
>> ______________________________**_________________
>> Perldl mailing list
>> [email protected]
>> http://mailman.jach.hawaii.**edu/mailman/listinfo/perldl<http://mailman.jach.hawaii.edu/mailman/listinfo/perldl>
>>
>
>
>
> --
>  "Debugging is twice as hard as writing the code in the first place.
>   Therefore, if you write the code as cleverly as possible, you are,
>   by definition, not smart enough to debug it." -- Brian Kernighan
>



-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan
_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to