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

Hi there fellow Monks and Monkettes!
I am having a bad day today ... too hot in this office not to have any air conditioning. Anyway .. I have a PDL matrix as shown below.
2.8017 0 0 0 0 0 0 0 1.2969 0 0 0 0 0 0 0 0.5495 0 0 0 0 0 0 0 0.2329 0 0 0 0 0 0 0 0.1953 0 0 0 0 0 0 0 0.1723 0 0 0 0 0 0 0 0.0959
What I need to do is expand this matrix to make it 35 by 35 square .. including what I have above as the top 7 by 7 squares and the rest of the 35 by 35 matrix populated by zeros.
The only way I can think of doing this thus far is to extract the data from the current 7 by 7 matrix and creating a whole new 35 by 35 one, zeros included, in a for loop.
Would anyone have any better ideas? Any suggestions much appreciated.

Replies are listed 'Best First'.
Re: expanding a PDL matrix
by reasonablekeith (Deacon) on Sep 07, 2005 at 15:36 UTC
    it's hard to give you a good solution without knowing what format you're storing your array in.

    Anyway the following does all you ask for, and was inspired by your first response. I would point out that slurping data like this gives funny x,y coordinates when accessing elements from the array (but makes it easy to slurp and pring), eg...

    $arr[y][x_from_top]
    here's the full code...
    #!/usr/bin/perl -w my @matrix1; while (<DATA>) { push @matrix1, [ split(/\s+/) ]; } my @matrix2 = empty_matrix(35,35); overwrite_matrix(\@matrix2, \@matrix1, 0, 0); print get_matrix(\@matrix1); print "\n"; print get_matrix(\@matrix2); #============================================================== sub empty_matrix { map {[ map{0}(1..$_[1]) ]} (1..$_[0]) } #============================================================== sub get_matrix { join("\n", map { join(",", @$_) } @{$_[0]} ) } #============================================================== sub overwrite_matrix { my ($array_ref1, $array_ref2, $x_offset, $y_offset) = @_; my $y = 0; foreach my $row_ref (@$array_ref2) { my $x = 0; foreach my $value (@$row_ref) { $array_ref1->[$y + $y_offset][$x + $x_offset] = $value; $x++; } $y++; } } __DATA__ 2.8017 0 0 0 0 0 0 0 1.2969 0 0 0 0 0 0 0 0.5495 0 0 0 0 0 0 0 0.2329 0 0 0 0 0 0 0 0.1953 0 0 0 0 0 0 0 0.1723 0 0 0 0 0 0 0 0.0959
    ---
    my name's not Keith, and I'm not reasonable.
Re: expanding a PDL matrix
by svenXY (Deacon) on Sep 07, 2005 at 14:36 UTC
    Hi,
    how about this:
    #!/usr/bin/perl -w use strict; my @line; my $k = 0; while (<DATA>) { chomp; $line[++$k] = [ split(/\s+/) ]; } my $output = ''; for (my $i = 1; $i <= 35; ++$i) { for (my $j = 1; $j <= 35; ++$j) { if ( $line[$i][$j] ) { $output .= $line[$i][$j] . "\t"; } else { $output .= "0\t"; } } $output .= "\n"; } print $output; __DATA__ 2.8017 0 0 0 0 0 0 0 1.2969 0 0 0 0 0 0 0 0.5495 0 0 0 0 0 0 0 0.2329 0 0 0 0 0 0 0 0.1953 0 0 0 0 0 0 0 0.1723 0 0 0 0 0 0 0 0.0959
    Cheers,
    Sven
Re: expanding a PDL matrix
by Angharad (Pilgrim) on Sep 07, 2005 at 16:28 UTC
    Hi there
    Thanks for the suggestions thus far. I'm not really using an array though ... I'm using a PDL matrix ... as follows:
    [ [ 2.7650514 0 0 0 0 + 0 0] [ 0 1.2905497 0 0 0 + 0 0] [ 0 0 0.54930597 0 0 + 0 0] [ 0 0 0 0.23264228 0 + 0 0] [ 0 0 0 0 0.19463077 + 0 0] [ 0 0 0 0 0 0.16669 +291 0] [ 0 0 0 0 0 + 0 0.095157451] ]
    And I've suddenly realised that what I actually want (sorry guys, I know its rude to chage the 'specs' after the initial posting) is a 7 row by 35 column PDL matrix ... where rows like this
    [ 0 0 0 0 0 0 0]
    are attached to the bottom of the matrix above .. so that the end result is a 7 row by 35 column PDL matrix. Does that make sense? :)