Search code examples
perlpdl

Perl PDL : How to change an value in a matrix


I want to change a value in a PDL matrix :

ex :

my $matrix= pdl [[1,2,3],[4,5,6]];
$matrix->at(0,0)=0;

But this is not working...

Thank you for your help


Solution

  • Here is one approach using range and the .= assignment operator :

    my $matrix= pdl [[1,2,3],[4,5,6]];
    print $matrix;
    $matrix->range([0,0]) .= 0;
    print $matrix;
    

    Output:

    [
     [1 2 3]
     [4 5 6]
    ]
    
    [
     [0 2 3]
     [4 5 6]
    ]
    

    Here is a recent quick introduction to PDL.