perlperl-modulepdl

Importing matrix from text file to PDL


Rather than creating the matrix using PDL, I would like to know ways to use existing matrix in TAB delimited file in PDL.
I wish to perform math operations on that matrix.
I tried math operations on randomly created matrix in situ but I have no idea if it is possible to import matrix from text files.

use PDL;
$a = zeroes 5,5;
$b = xvals $a;
print $b;

Here , 5X5 matrix with zeros was created for trial. I would be interested in external matrix not this one.


Solution

  • Use PDL::IO::Misc::rcols:

    Input data:

    1   2   3   4   5
    6   7   8   9   10
    

    code:

    use PDL;
    
    my $x = rcols( 'foo.dat', [] );
    
    print $x;
    

    Result:

    % perl foo.pl
    
    [
     [ 1  6]
     [ 2  7]
     [ 3  8]
     [ 4  9]
     [ 5 10]
    ]