in reply to local and global variable problem
Although I do not really see how this might have an impact on you program, it might be useful to see how you declare the @matrix_numbers array. Just as a matter of check. But my best guess is that you are not visiting your array data structure correctly. The Data::Dumper module might help figuring out what your data looks like.
In Section Seekers of Perl Wisdom