Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Manipulating columns of PDL data using slice() method

by Anonymous Monk
on Jul 02, 2007 at 22:17 UTC ( #624560=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am using the PDL module to perform statistical analysis on some basic datasets that I have.

I am having trouble figuring out how to do a simple operation (I think). I want to extract the minimum value of each COLUMN of data in a 2-dimensional PDL structure.

Here is a code fragment that I tried but didn't work:

for(my $k=0; $k<$maxColumns; $k++) { print "Min: " . min($dataA->slice('$k,:')) . "\n\n"; print "Max: " . max($dataB->slice('$k,:')) . "\n\n"; }
The variable "$k" can't be interpreted within the slice() method.

Is there a way to perform a set operation to just get the results I want without a FOR loop?

Any help is appreciated. Thanks.

Replies are listed 'Best First'.
Re: Manipulating columns of PDL data using slice() method
by levien (Initiate) on Jul 03, 2007 at 00:26 UTC
    The following should probably do what you want:
    minimum($dataA->xchg(0,1)); maximum($dataB->xchg(0,1));
    minimum() and maximum() take the minimum and maximum values over the first dimension. As you need the these values from the second dimension, you can use xchg to swap dimensions.

    See also: http://pdl.sourceforge.net/PDLdocs/Ufunc.html#minimum

    Your original code should also work, if you use double quotes instead of single ones:

    for(my $k=0; $k<$maxColumns; $k++) { print "Min: " . min($dataA->slice("$k,:")) . "\n\n"; print "Max: " . max($dataB->slice("$k,:")) . "\n\n"; }
    -L.
      Thanks for the info. I ended up using maximum(transpose($dataA)) as my solution. Similar to the xchg method probably.
Re: Manipulating columns of PDL data using slice() method
by lin0 (Curate) on Jul 02, 2007 at 22:38 UTC

    Hi Anonymous Monk

    I recommend you to have a look at the following nodes for information on PDL

    Now, answering your question, you could use the function minimum together with xchg as in the following example:

    #!/usr/bin/perl use warnings; use strict; use PDL; my ( @data, @tmp ); while (defined(my $line = <DATA>)) { chomp ($line); @tmp = split /\s+/, $line; push @data, [ @tmp ]; } my $patterns = pdl(@data); my $minimum = minimum($patterns->xchg(0,1)); print "minimum: $minimum\n"; __DATA__ 2.0 4.0 4.0 5.0 5.0 4.0 5.5 6.0 5.0 5.0 4.5 4.5 5.0 5.5 5.5 5.0 5.0 4.5 4.5 5.0 9.5 -1.0 9.0 9.5 8.0 8.0 7.0 8.0 8.0 7.0 8.5 7.0 7.0 8.5 7.0 7.0 7.5 7.0 6.5 8.0 8.0 6.5 6.5 7.0 10.0 10.0 10.0 9.0 10.0 9.0 9.5 10.0 8.0 10.0 9.5 9.5 9.0 9.0 9.0 10.0

    The output is:

    minimum: [2 -1]

    Cheers,

    lin0

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://624560]
Approved by lin0
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2020-07-13 15:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?