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

Dear Monks, appreciate if can help me in converting a Column matrix to a row matrix using Math::MatrixReal module

use Math::MatrixReal; # my matrix is below as input my $mref = [ [ '5', '4', '4', '2', '4' ], ]; my $matrix = Math::MatrixReal->new_from_rows( $mref ); print $matrix; # I want to convert above into below ( 5 Rows x 1 Column) # As I need this for later on to use for further multiplication purpos +e [ [ '5'], [ '4' ], [ '4'], [ '2'], [ '4'], ];

Replies are listed 'Best First'.
Re: Convert Column Matrix into Row Matrix using Math::MatrixReal
by Laurent_R (Canon) on May 16, 2014 at 19:03 UTC
    Hmm, not using your module, you could try something simple in the spirit of this code under the debugger.
    DB<2> $mref = [ qw / 5 4 4 2 4/ ]; DB<3> x \$mref 0 REF(0x600500ac8) -> ARRAY(0x600509918) 0 5 1 4 2 4 3 2 4 4 DB<4> $nref = [ map {[$_]} @$mref ]; DB<5> x \$nref 0 REF(0x600500708) -> ARRAY(0x6005e8c88) 0 ARRAY(0x6005ea970) 0 5 1 ARRAY(0x6005f3d20) 0 4 2 ARRAY(0x6004bdfd0) 0 4 3 ARRAY(0x60060d270) 0 2 4 ARRAY(0x6005e8c40) 0 4
    Note that I used an simple arrayref as source data, as I did not see the reason to have an array ref to an array ref if the first array as only one element. But that is quite easy to change if this is really what you need.

    Edit: The post by 2teez below reminded me that the Data Dumper module gives an output somewhat cleaner than the x function of the debugger. Since I had still my debugging screen as above, with the data structures still there, this is the data dumper output:

    DB<6> use Data::Dumper; DB<7> print Dumper \$nref $VAR1 = \[ [ '5' ], [ '4' ], [ '4' ], [ '2' ], [ '4' ] ];

    And if the input data structure is really a ref to an AoA, the code can be changed as follows:

    DB<8> $mref = [ [ qw / 5 4 4 2 4/ ], ]; DB<9> $nref = [ map {[$_]} map {@{$_}} @$mref ];
    producing the same resulting data structure and output as above.
Re: Convert Column Matrix into Row Matrix using Math::MatrixReal
by 2teez (Vicar) on May 16, 2014 at 19:10 UTC

    I have not used Math::MatrixReal before and am press for time to go through it now, but am sure the module can convert from column to row. If so, prepare your data in the way you wanted like so:

    use warnings; use strict; use Data::Dumper; my $mref = [ [ '5', '4', '4', '2', '4' ],]; my @data; for (@$mref){ push @data, map {[$_]} @{$_} } print Dumper \@data;
    and make the the module do the rest.
    Output
    $VAR1 = [ [ '5' ], [ '4' ], [ '4' ], [ '2' ], [ '4' ] ];
    However, if the module does this, it might be cleaner and perhaps faster.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Convert Column Matrix into Row Matrix using Math::MatrixReal
by wjw (Priest) on May 16, 2014 at 22:16 UTC
    Took a look at Math::MatrixReal Here and found the following which ran for me.
    #!/usr/bin/perl -w use Math::MatrixReal; $matrix = Math::MatrixReal->new_from_string(<<'MATRIX'); [ 1 2 3 ] MATRIX # was a 3x2 matrix in original example, removed these [ 4 5 6 ] print $matrix . "\n\n"; #added this to example $matrix2 = Math::MatrixReal->new(3,1); #again, original was a 3x2 matr +ix $matrix2->transpose($matrix); print $matrix2;

    Output:

    [ 1.000000000000E+00 2.000000000000E+00 3.000000000000E+00 ] [ 1.000000000000E+00 ] [ 2.000000000000E+00 ] [ 3.000000000000E+00 ]

    Hope that is helpful

    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results...