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

Hello Monks !

Need help on below Math::MatrixReal module

1) How to access an individual element and print it out of the final matrix <\p>

2) How to print final matrix in an Array so that I can use it later on for other purpose, printing to screen and also printing to a csv file

3) How to print final matrix in an Hash if 2D matrix so that I can print to screen and also save to CSV

Code =====

use warnings; use strict; use Net::FTP; use Math::MatrixReal; print "\n Matrix from Arrays -Printing in ROWS format\n"; my @matrix1 = ( -0.022333, -0.016800, -0.016128, -0.015456, -0.014784, -0.014112, -0.013440, -0.012768, -0.012096, -0.011424 ); my @matrix2 = ( 3.36, 3.2, 3.35, 3.45, 3.55, 3.65, 3.75, 3.85, 3.95, 2.30 ); my $matrix_final = Math::MatrixReal->new_from_rows( [ \@matrix1, \@mat +rix2 ] ); print $matrix_final; my $matrix_transpose = Math::MatrixReal->new(10,2); $matrix_transpose->transpose($matrix_final); print $matrix_transpose;
  • Comment on How to output matrix data from Math:;MatrixReal into a Hash or an Array
  • Download Code

Replies are listed 'Best First'.
Re: How to output matrix data from Math:;MatrixReal into a Hash or an Array
by wjw (Priest) on May 09, 2014 at 21:25 UTC
    I suggest you use 'perl -d (yourscript.pl)' to take a look (>x $matrix_final) at what those two references are made of.
    Once you do so, it will become apparent to you how to do all three of the things you want.
    Both $matrix_final and $matrix transpose are simply references to arrays of arrays. The basic perldoc (perldsc I think) will show you how to do what you want to do... .

    That should get you started. With a little work you will have what you want shortly...

    Hope that is helpful...

    Update:

    Added link to perldsc to be a bit more helpful...

    Update: rev b Looking at Math::MatrixReal it seems that most of what you want to do is actually supplied by the module...
    It will be helpful to actually read that...
    In particular, $new_matrix = Math::MatrixReal->new_from_string($string); This method allows you to read in a matrix from a string (for instance, from the keyboard, from a file or from your code).

    ...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...
Re: How to output matrix data from Math:;MatrixReal into a Hash or an Array
by Laurent_R (Canon) on May 10, 2014 at 09:07 UTC
    Yes, use the debugger "x" command to visualize the structure of $matrix_transpose, as suggested by wjw. Or add the following line near the top of your program:
    use Data::Dumper;
    and change the last line to:
    print Dumper $matrix_transpose;
    and then post here the output of that print command.

      Folks, Thanks for your answers. When I print a particular matrix I get following Data dumper format. This is a 5 x 5 matrix. I would like to print it to csv file like below.

      where each 1,1,1,1,1 is my first column A in csv then B ...

      A B C D E 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

      print Dumper $matrix; $VAR1 = bless( [ [ [ '5', '4', '4', '2', '4' ], [ '9', '6', '4', '4', '3' ], [ '2', '73', '96', '6', '8' ], [ '2', '4', '9', '87', '8' ], [ '2', '4', '10', '6', '8' ], ], 5, 5 ], 'Math::MatrixReal' );

      Appreciate your help if you can give me the code for this matrix printing to a output file like csv. </code>

        The documentation for the Math::MatrixReal module discusses the element method, and the row method. Since the row method (inconveniently, in this case) generates a new 1xN matrix object, it's not as useful for file dumps as "element", which just returns plain old values.

        All you really have to do is iterate over the rows, over the columns, and build up a CSV file. I chose to use Text::CSV in this solution, but it's so trivial that it would probably be easier and no less maintainable to just generate your own CSV:

        use Text::CSV; use Math::MatrixReal; my $mref = [ [ '5', '4', '4', '2', '4' ], [ '9', '6', '4', '4', '3' ], [ '2','73','96', '6', '8' ], [ '2', '4', '9','87', '8' ], [ '2', '4','10', '6', '8' ], ]; my $matrix = Math::MatrixReal->new_from_rows( $mref ); # At this point $matrix is a Math::MatrixReal object that # resembles the one you showed us a Data::Dumper dump of. my $csv = Text::CSV->new; foreach my $row_num ( 1 .. 5 ) { $csv->print(*STDOUT, [ map { $matrix->element($row_num,$_) } 1 .. 5 +] ); print {*STDOUT} "\n"; }

        Substitute *STDOUT for your own output file handle, of course.


        Dave

Re: How to output matrix data from Math:;MatrixReal into a Hash or an Array
by Laurent_R (Canon) on May 13, 2014 at 09:30 UTC
    Since you are dealing with an object, you should use the methods provided by your class to retrieve the values that you need (as suggested by davido in his post). If it were a reference to a simple AoA (not a blessed object), then you could have something like this:
    $perl -e ' > use strict; > use warnings; > > > my $mref = [ [ '5', '4', '4', '2', '4' ], > my $mref = [ > [ '5', '4', '4', '2', '4' ], > [ '9', '6', '4', '4', '3' ], > [ '2','73','96', '6', '8' ], > [ '2', '4', '9','87', '8' ], > [ '2', '4','10', '6', '8' ], > ]; > > for my $rows (@$mref) { > local $" = "\t"; > print "@$rows \n"; > } > ' 5 4 4 2 4 9 6 4 4 3 2 73 96 6 8 2 4 9 87 8 2 4 10 6 8