How can I operate with the matrix without using the matrix library in Perl?

I need to write a matrix in Perl without using the library for it. How could I implement the operations below in my code? - transpose the matrix - calculate the average of each row and column, (I tried to make it in the script below) - find the smallest value in the whole matrix, - find the index of the row with the smallest sum of values.

I use this code for matrix operating, it works, but I completely do not understand how to perform these operations. Thank you in advance

#!usr/bin/perl 
use strict; 
use warnings; 



my(@MatrixA, @MatrixB, @Resultant) = ((), (), ()); 

# Asking for User Input for Matrix A 
print"Let's begin with the matrix A dimensions\n"; 
print"\tPlease, enter how many rows should Matrix A have:"; 
chomp(my$rowA= <>);  # CHOMP TO TAKE USER INPUT 
print"\tPlease, enter how many columns should Matrix A have:"; 
chomp(my$columnA= <>); 

# Asking for User Input for Matrix B 
print"Then we have matrix B:\n"; 
print"\tPlease, enter how many rows should Matrix B have:"; 
chomp(my$rowB= <>); 
print"\tPlease, enter how many columns should Matrix B have:"; 
chomp(my$columnB= <>); 

# Asking User to input elements of matrices 
if($rowA== $rowB and $columnA== $columnB) #checking the dimensions whether they match
{ 
    print"Enter $rowA * $columnA elements in MatrixA:\n";  
    foreach my $m(0..$rowA- 1) 
    { 
        foreach my $n(0..$columnA- 1) 
        { 
            chomp($MatrixA[$m][$n] = <>);  # reading the values
        } 
    } 

    print"Enter $rowB * $columnB elements in MatrixB:\n";  
    foreach my$m(0..$rowB- 1) 
    { 
        foreach my $n(0..$columnB- 1) 
        { 
            chomp($MatrixB[$m][$n] = <>);  # reading the values 
        } 
    } 

    # Performing Addition operation 
    foreach my $m(0..$rowB- 1) 
    { 
        foreach my $n(0..$columnB- 1) 
        { 
            $Resultant[$m][$n] = $MatrixA[$m][$n] +  
                                 $MatrixB[$m][$n]; 
        } 
    } 

    # Printing Matrix A 
    print"MatrixA is :\n";  
    foreach my $m(0..$rowB- 1) 
    { 
        foreach my $n(0..$columnB- 1) 
        { 
            print"$MatrixA[$m][$n] "; 
        } 
        print"\n"; 
    } 

    # Printing Matrix B 
    print"MatrixB is :\n";  
    foreach my$m(0..$rowB- 1) 
    { 
        foreach my$n(0..$columnB- 1) 
        { 
            print"$MatrixB[$m][$n] "; 
        } 
        print"\n"; 
    } 

    # Printing the sum of Matrices 
    print"SUM of MatrixA and MatrixB is :\n";  
    foreach my $m(0..$rowB- 1) 
    { 
        foreach my $n(0..$columnB- 1) 
        { 
            print"$Resultant[$m][$n] "; 
        } 
        print"\n"; 
    } 

    # Printing the average of Matrices 
    print"The average of Matrices (row) :\n";  
    foreach my $m(0..$rowB- 1) 
    { 
        my $sum_r += $m;
        my $average_r = $sum_r/$rowB;
        print "Average is $average_r\n";
            print"\n";        
    }
    print"The average of Matrices (column) :\n";
    foreach my $n(0..$columnB- 1) 
          { 
        my $sum_c += $n;
        my $average_c = $sum_c/$columnB;
        print "Average is $average_c\n";
            print"\n";
          }     
} 

# Error if Matrices are of different order 
else
{ 
    print"Matrices order does not MATCH, Addition is not Possible"; 
} 

Upvotes: 1

Views: 101

Answers (1)

H&#229;kon H&#230;gland
H&#229;kon H&#230;gland

Reputation: 40758

Here is an example of how to transpose the matrix:

my @MatrixA = ([1,2,3], [4, 5, 6]);
my @MatrixA_T = transpose(@MatrixA);

sub transpose {
    my (@M) = @_;

    my @MT;
    for my $i (0..$#M) {
        for my $j (0..$#{$M[0]}) {
            $MT[$j][$i] = $M[$i][$j];
        }
    }
    return @MT;
}

Note that matrix operations are already implemented if you use the PDL module.

Upvotes: 1

Related Questions