Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^5: How to output matrix data from Math:;MatrixReal into a Hash or an Array

by davido (Cardinal)
on May 13, 2014 at 13:58 UTC ( [id://1085905]=note: print w/replies, xml ) Need Help??


in reply to Re^4: How to output matrix data from Math:;MatrixReal into a Hash or an Array
in thread How to output matrix data from Math:;MatrixReal into a Hash or an Array

It's not documented in the pod, but my own test runs indicated that the elements accessor is 1-based. Maybe that's how mathematics people like their matrices indexed... I dont know. But it seems to be how this module does it. Using zero will throw an out of range error.


Dave

  • Comment on Re^5: How to output matrix data from Math:;MatrixReal into a Hash or an Array

Replies are listed 'Best First'.
Re^6: How to output matrix data from Math:;MatrixReal into a Hash or an Array
by Laurent_R (Canon) on May 13, 2014 at 15:37 UTC
    OK, Dave, thanks. I had looked into the POD and also did not see anything saying that indexing should be 1-based, that's why I was wondering and asked the question. But I did not have time then to try to install the module and test it. Thanks again.
Re^6: How to output matrix data from Math:;MatrixReal into a Hash or an Array
by John007 (Acolyte) on May 15, 2014 at 04:27 UTC

    Folks thank you very much for your excellent help here. Truly appreciated.

    I modified the code slightly to send data to a csv file as below.

    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; open my $FILE, ">", "./whatever.csv"; foreach my $row_num ( 1 .. 5 ) { $csv->print(*STDOUT, [ map { $matrix->element($row_num,$_) } 1 .. 5 +] ); print {*STDOUT} "\n"; $csv->print($FILE, [ map { $matrix->element($row_num,$_) } 1 .. 5 ] +); print $FILE "\n"; }

      One possible bug I see: print {*STDOUT}, "\n";.

      In the code I provided earlier in this thread, the $csv->print(....) was directing to STDOUT because I didn't want to set up an output file in an example script. Also, the newline was explicitly output because I noticed that one wasn't being inserted between CSV rows automatically by $csv->print(...).

      Now in your solution you've got $csv->print($FILE...), but haven't directed the "print {*STDOUT} "\n"; to $FILE instead.

      So you may want to look at the CSV it's outputting and verify that there's a newline being created between what you intend to be CSV rows. It may not be happening.


      Dave

        Thank you very much for your help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1085905]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 10:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found