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

When using Math::Matrix, how can I get values at certain positions in matrix?
Only thing I find in documentation is print (for printing entire matrix, which doesn't help me) and slice (for taking certain columns, but result is also Math::Matrix so it doesn't help)...
What did I miss? How to get one element from matrix?
  • Comment on Math::Matrix and getting matrix elements

Replies are listed 'Best First'.
Re: Math::Matrix and getting matrix elements
by kennethk (Abbot) on Apr 28, 2010 at 14:41 UTC
    A quick look at the source says that the Math::Matrix objects are blessed arrays of arrays. This means you should be able to access the value of A(i, j) with the syntax $matrix->[i][j]. See perlreftut or perllol for more information. It does not look like the author provided getter/setter routines.
Re: Math::Matrix and getting matrix elements
by toolic (Bishop) on Apr 28, 2010 at 14:54 UTC
    Math::Matrix returns an object, and you can use arrow syntax to access individual elements:
    use strict; use warnings; use Data::Dumper; use Math::Matrix; srand(time); my $a = new Math::Matrix ([rand,rand,rand], [rand,rand,rand], [rand,rand,rand]); print Dumper($a); print $a->[0]->[0]; # get 1 element __END__ $VAR1 = bless( [ [ '0.83428955078125', '0.442596435546875', '0.792205810546875' ], [ '0.193817138671875', '0.21514892578125', '0.23980712890625' ], [ '0.09869384765625', '0.627197265625', '0.514892578125' ] ], 'Math::Matrix' ); 0.83428955078125

    It is often helpful to look at the tests provided with the CPAN distribution to see example usage code (follow the MANIFEST link to the "t/" links). In this case, the tests do show how to access individual elements.

    Super Search is another efficient way of finding answers with code samples:
    accessing elements inside Math::Matrix