You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Perl中不使用矩阵库实现指定矩阵操作

Hey there! Let's work through each of your matrix operation requests step by step, using your existing Perl code as a base. I'll explain each fix/addition clearly so you understand how it works.


1. Matrix Transposition

Transposing a matrix swaps its rows with columns—so the element at position (m,n) moves to (n,m). Here's how to implement this for your MatrixA (you can replicate this logic for MatrixB or the sum matrix if needed):

# Matrix Transposition for MatrixA
my @TransposedA;
foreach my $m (0..$rowA-1) {
    foreach my $n (0..$columnA-1) {
        $TransposedA[$n][$m] = $MatrixA[$m][$n];
    }
}

# Print Transposed Matrix A
print "\nTransposed MatrixA is:\n";
foreach my $row (@TransposedA) {
    print join(" ", @$row) . "\n";
}

2. Fix Row & Column Averages

Your current code was adding row/column indices (like $m) instead of the actual matrix elements—no wonder it was giving incorrect results! Let's rewrite this to calculate sums of elements properly:

Row Averages

For each row, sum all elements in that row, then divide by the number of columns:

print "\nRow Averages of MatrixA:\n";
foreach my $m (0..$rowA-1) {
    my $row_sum = 0;
    foreach my $n (0..$columnA-1) {
        $row_sum += $MatrixA[$m][$n];
    }
    my $row_avg = $row_sum / $columnA;
    printf "Row %d average: %.2f\n", $m+1, $row_avg; # +1 for 1-based index if you prefer that
}

Column Averages

For each column, sum elements across all rows, then divide by the number of rows:

print "\nColumn Averages of MatrixA:\n";
foreach my $n (0..$columnA-1) {
    my $col_sum = 0;
    foreach my $m (0..$rowA-1) {
        $col_sum += $MatrixA[$m][$n];
    }
    my $col_avg = $col_sum / $rowA;
    printf "Column %d average: %.2f\n", $n+1, $col_avg;
}

3. Find Minimum Element in the Matrix

We'll initialize the minimum value with the first element of the matrix, then iterate through every element to check if we find a smaller value:

# Find minimum element in MatrixA
my $min_val = $MatrixA[0][0];
foreach my $row (@MatrixA) {
    foreach my $elem (@$row) {
        $min_val = $elem if $elem < $min_val;
    }
}
print "\nMinimum element in MatrixA: $min_val\n";

4. Find Index of Row with Smallest Sum

We'll track the smallest sum and its corresponding row index as we calculate each row's total:

# Find row index with smallest sum in MatrixA
my $smallest_sum = undef;
my $smallest_row_idx = 0;

foreach my $m (0..$rowA-1) {
    my $row_sum = 0;
    foreach my $n (0..$columnA-1) {
        $row_sum += $MatrixA[$m][$n];
    }
    # Initialize smallest_sum with first row's sum if not set
    if (!defined $smallest_sum || $row_sum < $smallest_sum) {
        $smallest_sum = $row_sum;
        $smallest_row_idx = $m;
    }
}
printf "\nRow with smallest sum (0-based index %d) has total: %d\n", $smallest_row_idx, $smallest_sum;

Full Modified Code

Here's how all these features fit into your original code (added after the matrix sum section, inside the if block where dimensions match):

#!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"	Please, enter how many rows should Matrix A have:";
chomp(my$rowA= <>);
print"	Please, 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"	Please, enter how many rows should Matrix B have:";
chomp(my$rowB= <>);
print"	Please, 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";
    }

    # --------------------------
    # New Operations Start Here
    # --------------------------

    # 1. Matrix Transposition for MatrixA
    my @TransposedA;
    foreach my $m (0..$rowA-1) {
        foreach my $n (0..$columnA-1) {
            $TransposedA[$n][$m] = $MatrixA[$m][$n];
        }
    }
    print"\nTransposed MatrixA is:\n";
    foreach my $row (@TransposedA) {
        print join(" ", @$row) . "\n";
    }

    # 2. Row Averages of MatrixA
    print"\nRow Averages of MatrixA:\n";
    foreach my $m (0..$rowA-1) {
        my $row_sum = 0;
        foreach my $n (0..$columnA-1) {
            $row_sum += $MatrixA[$m][$n];
        }
        my $row_avg = $row_sum / $columnA;
        printf "Row %d average: %.2f\n", $m+1, $row_avg;
    }

    # 2. Column Averages of MatrixA
    print"\nColumn Averages of MatrixA:\n";
    foreach my $n (0..$columnA-1) {
        my $col_sum = 0;
        foreach my $m (0..$rowA-1) {
            $col_sum += $MatrixA[$m][$n];
        }
        my $col_avg = $col_sum / $rowA;
        printf "Column %d average: %.2f\n", $n+1, $col_avg;
    }

    # 3. Find Minimum Element in MatrixA
    my $min_val = $MatrixA[0][0];
    foreach my $row (@MatrixA) {
        foreach my $elem (@$row) {
            $min_val = $elem if $elem < $min_val;
        }
    }
    print"\nMinimum element in MatrixA: $min_val\n";

    # 4. Find Row with Smallest Sum in MatrixA
    my $smallest_sum = undef;
    my $smallest_row_idx = 0;
    foreach my $m (0..$rowA-1) {
        my $row_sum = 0;
        foreach my $n (0..$columnA-1) {
            $row_sum += $MatrixA[$m][$n];
        }
        if (!defined $smallest_sum || $row_sum < $smallest_sum) {
            $smallest_sum = $row_sum;
            $smallest_row_idx = $m;
        }
    }
    printf "\nRow with smallest sum (0-based index %d) has total: %d\n", $smallest_row_idx, $smallest_sum;

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

内容的提问来源于stack exchange,提问作者Павел Костарев

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:07:03