Biotechnology Forums

Full Version: PERL Program - Matrix Transpose
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
print "Enter 3*3 Array Elements : ";
for($row=0; $row<3 ; $row++) 
{
  for($col=0; $col<3 ; $col++) 
  {
    $matrix[$row][$col] = <STDIN>;
  }
}

# Print the original matrix
print "Entered Matrix is : \n";
for($row=0; $row<3 ; $row++) 
{
  for($col=0; $col<3 ; $col++) 
  {
    printf("%3d", $matrix[$row][$col]);
  }
  print "\n";
}

#Transposing the matrix
for($row=0; $row<3 ; $row++) 
{
  for($col=0; $col<3 ; $col++) 
  {
    $matrix_transpose[$row][$col] = $matrix[$col][$row];
  }
}

# Print the matrix in 3 integer spaces : "%3d"
print "Transpose of the Matrix is :\n";
for($row=0; $row<3 ; $row++) 
{
  for($col=0; $col<3 ; $col++) 
  {
    printf("%3d", $matrix_transpose[$row][$col]);
  }
  print "\n";
}

# Check for Symmetric Matrix
$isSymmetric = 0;
for($row=0; $row<3 ; $row++) 
{
  for($col=0; $col<3 ; $col++) 
  {
if ($matrix[$row][$col] != $matrix[$col][$row])
{ $isSymmetric = 1; print $isSymmetric; }
  }
}
if($isSymmetric==0)
{ print("Matrix is symmetric. \n"); }
else
{ print("Matrix is not symmetric. \n"); }