Laurent_R
Veteran
/ Moderator
May 29, 2015, 1:48 PM
Post #3 of 4
(2354 views)

Re: [sgupta] program to compute the transpose of a 3X3 matrix
[In reply to]

Can't Post


All right, there are some more clever ways to do it in Perl, but the following implements a very simple algorithm following the basic definition of matrix transposition:
use strict; use warnings; use Data::Dumper; my @matrix = ( [ qw /A1 A2 A3/], [ qw /B1 B2 B3/], [ qw /C1 C2 C3/] ); print "Original matrix: \n", Dumper \@matrix; my @transposed; for my $i (0..2) { for my $j (0..2) { $transposed[$i][$j] = $matrix[$j][$i]; } } print "Transposed matrix: \n", Dumper \@transposed; This is the output:
$ perl transpose.pl Original matrix: $VAR1 = [ [ 'A1', 'A2', 'A3' ], [ 'B1', 'B2', 'B3' ], [ 'C1', 'C2', 'C3' ] ]; Transposed matrix: $VAR1 = [ [ 'A1', 'B1', 'C1' ], [ 'A2', 'B2', 'C2' ], [ 'A3', 'B3', 'C3' ] ];
