Matrices in Perl 
Author Message
 Matrices in Perl


        [1, 2, 3 ],
        [4, 5, 6 ],
        [7, 8, 9]
);
$matrix [1]  [2] = 100;

Why would the value of 6 at row 1, column 2 in the matrix change to
100?  Why wouldn't the answer be a change of value 2 in the matrix?

(2)
Why would the following created the answer (matrix) below?
$matrix {0} {2} = 100;
$matrix {1} {0} = 200;
$matrix {2} {1} = 300;

answer
[0, 0, 100]
[200, 0, 0]
[0, 300, 0]

Kevin Bass



Sun, 27 Feb 2000 03:00:00 GMT  
 Matrices in Perl


: (1)

:       [1, 2, 3 ],
:       [4, 5, 6 ],
:       [7, 8, 9]
: );
: $matrix [1]  [2] = 100;
:
: Why would the value of 6 at row 1, column 2 in the matrix change to
: 100?  Why wouldn't the answer be a change of value 2 in the matrix?

'cuz indexing begins with zero. The value 2 is in the zeroth row,
first position.

: (2)
: Why would the following created the answer (matrix) below?
: $matrix {0} {2} = 100;
: $matrix {1} {0} = 200;
: $matrix {2} {1} = 300;
:
: answer
: [0, 0, 100]
: [200, 0, 0]
: [0, 300, 0]

I would think you'd get a hash of hashes rather than a list of array
refs, and that there'd be undefs rather than zeros, but otherwise it's
the same situation.

dan

--
Daniel Macks


http://www.netspace.org/~dmacks



Sun, 27 Feb 2000 03:00:00 GMT  
 Matrices in Perl

Thanks for answering my post.  I understand the matrix concept.  
Sometimes, when learning a different language, things are not
understood right away but that's life.  



Sun, 27 Feb 2000 03:00:00 GMT  
 Matrices in Perl

Hello -

Quote:

>(1)

>    [1, 2, 3 ],
>    [4, 5, 6 ],
>    [7, 8, 9]
>);
>$matrix [1]  [2] = 100;
>Why would the value of 6 at row 1, column 2 in the matrix change to
>100?  Why wouldn't the answer be a change of value 2 in the matrix?

Because $matrix[1] is a list reference.  The list to which
it is a reference has three elements, initially: (4,5,6).
You then assign 100 to element 2.

Note that you could also write: $matrix[1]->[2] = 100; which
more graphically displays the dereferencing of the list
reference.

Quote:
>(2)
>Why would the following created the answer (matrix) below?
>$matrix {0} {2} = 100;
>$matrix {1} {0} = 200;
>$matrix {2} {1} = 300;
>answer
>[0, 0, 100]
>[200, 0, 0]
>[0, 300, 0]

I'm not sure what you mean here.  The assignments you've
written create a hash called %matrix.  It has three keys.
Each of the corresponding values is an anonymous hash
reference.  Each of the anonymous hashes so referenced
has one key, and the values for those keys are 100, 200, 300.
What you've written under "answer" are expressions evaluating
to list anonymous list references - I'm not sure where that
came from, or where it fits in.  Maybe there's an intervening
step that could clarify the connection?

David Black



Sun, 27 Feb 2000 03:00:00 GMT  
 Matrices in Perl

Hi,

Quote:

> (1)

>         [1, 2, 3 ],
>         [4, 5, 6 ],
>         [7, 8, 9]
> );
> $matrix [1]  [2] = 100;

> Why would the value of 6 at row 1, column 2 in the matrix change to
> 100?  Why wouldn't the answer be a change of value 2 in the matrix?

Don't forget that array indexing starts at 0 (not at 1 which is the
usual case when working with matrices). Thus, $matrix[1][2] means
"row 2" and "column 3" (not "row 1" and "column 2").

Quote:
> (2)
> Why would the following created the answer (matrix) below?
> $matrix {0} {2} = 100;
> $matrix {1} {0} = 200;
> $matrix {2} {1} = 300;

> answer
> [0, 0, 100]
> [200, 0, 0]
> [0, 300, 0]

The lines above don't create something like a two-dimensional array
(like in (1)), but they just show another way of representing a matrix.
Using a hash you don't have to store all the zeroes of the matrix,
but only the non-zero elements. This means that whenever you want to
read an entry of the matrix you must check if the queried hash
keys exist; if they do you can read the element using a syntax like
"$elem = $matrix{2}{1}", if they don't exist you may say "$elem = 0"
(the main purpose of this representation is to save memory).

Hoep this helps, Eike
--
======================================================================
 Eike Grote, Theoretical Physics IV, University of Bayreuth, Germany
----------------------------------------------------------------------

 WWW    -> http://www.phy.uni-bayreuth.de/theo/tp4/members/grote.html
           http://www.phy.uni-bayreuth.de/~btpa25/
======================================================================



Sun, 27 Feb 2000 03:00:00 GMT  
 Matrices in Perl



+-----
| $matrix [1]  [2] = 100;
| Why would the value of 6 at row 1, column 2 in the matrix change to 100?
| Why wouldn't the answer be a change of value 2 in the matrix?
+--->8

Perl, as with just about every computer language except BASIC, starts array
numbering with 0.  Thus, the first entry in the matrix is at [0][0], not
[1][1].

If you really want 1-based arrays, you can set the special variable $[ to 1.
(But since BASIC is the "odd one out", you might be better off looking up
OPTION BASE in your BASIC manual....)

--

cleveland, ohio              mr/2 ice's "rfc guru" :-)           FORZA CREW!
Warpstock '97:  OS/2 for the rest of us!  http://www.warpstock.org



Sun, 27 Feb 2000 03:00:00 GMT  
 Matrices in Perl


Quote:
>Hello -


>>(1)

>>        [1, 2, 3 ],
>>        [4, 5, 6 ],
>>        [7, 8, 9]
>>);
>>$matrix [1]  [2] = 100;

>>Why would the value of 6 at row 1, column 2 in the matrix change to
>>100?  Why wouldn't the answer be a change of value 2 in the matrix?

>Because $matrix[1] is a list reference.  The list to which
>it is a reference has three elements, initially: (4,5,6).
>You then assign 100 to element 2.

>Note that you could also write: $matrix[1]->[2] = 100; which
>more graphically displays the dereferencing of the list
>reference.

Remember, list references start at 0

the number pairs of your matrix would be

0,0   0,1   0,2   <--- row 0
1,0   1,1   1,2   <--- row 1
2,0   2,1   2,2   <--- row 2
  |        |       |
col0 col1 col2

Therefore  $matrix[1][2] points to the SECOND row and the THIRD column, which
previously held the value 6.

- Show quoted text -

Quote:
>>(2)
>>Why would the following created the answer (matrix) below?
>>$matrix {0} {2} = 100;
>>$matrix {1} {0} = 200;
>>$matrix {2} {1} = 300;

>>answer
>>[0, 0, 100]
>>[200, 0, 0]
>>[0, 300, 0]

>I'm not sure what you mean here.  The assignments you've
>written create a hash called %matrix.  It has three keys.
>Each of the corresponding values is an anonymous hash
>reference.  Each of the anonymous hashes so referenced
>has one key, and the values for those keys are 100, 200, 300.
>What you've written under "answer" are expressions evaluating
>to list anonymous list references - I'm not sure where that
>came from, or where it fits in.  Maybe there's an intervening
>step that could clarify the connection?

>David Black


I believe Kevin is trying to create a two dimensional hash.  If you look at the
values he assigns to the hash, you see that they correspond to the row,value
positions of the matrix (Start counting from 0).

To my surprise, this actually works.

Try running the following

$matrix {0}{0} = 11;
$matrix {0}{1} = 12;
$matrix {0}{2} = 13;
$matrix {1}{0} = 21;
$matrix {1}{1} = 22;
$matrix {1}{2} = 23;
$matrix {2}{0} = 31;
$matrix {2}{1} = 32;
$matrix {2}{2} = 33;

print ("$matrix{0}{0} ");
print ("$matrix{0}{1} ");
print ("$matrix{0}{2}\n");
print ("$matrix{1}{0} ");
print ("$matrix{1}{1} ");
print ("$matrix{1}{2}\n");
print ("$matrix{2}{0} ");
print ("$matrix{2}{1} ");
print ("$matrix{2}{2}\n");

You will get

11 12 13
21 22 23
31 32 33

Which can be viewed as a matrix.

As far as I can understand, three hashes are created, one for each row.  Each
hash row has three elements which hold three values.

Am I interpreting this correctly?

Still a newbie and learning every day.

Stuart Knetsch

Remove the you know what from my address to send me E-mail



Sun, 27 Feb 2000 03:00:00 GMT  
 Matrices in Perl

On      Wed, 10 Sep 97 10:02:32 -0400, Brandon S. Allbery KF8NH; to

By doing that I figured you didn't want a reply.

Quote:
> Perl, as with just about every computer language except BASIC, starts array
> numbering with 0.

Whoops.  There's fortran and derivates, MatLab, and, I'm sure, others.

Quote:
> If you really want 1-based arrays, you can set the special variable $[ to 1.

This is heavily deprecated.  You'll break every piece of code you use.

Quote:
> (But since BASIC is the "odd one out", you might be better off looking up
> OPTION BASE in your BASIC manual....)

Indeed. :-)

--



Sun, 27 Feb 2000 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. matrix in perl

2. Math::Matrix and matrix inversion

3. Perl Matrix Filter Module like in Excel?

4. Matrix problem solving in perl

5. Matrix array in Perl Win32

6. Perl Portability Matrix

7. Problem in loading a matrix from a file

8. matching 2d matrices

9. PDL::Slatec Problem of eigenvalue of Matrix

10. Inverting Matrices?

11. REQ: .pm for matrix...

12. PDL::Slatec Eigenvalue of Matrix

 

 
Powered by phpBB® Forum Software