Find the difference in the matrix

Let's find the matrix difference in Perl. Think of the matrix as a column-first matrix.

# Matrix representation of mathematics
# 3 rows and 2 columns matrix
# 14
# twenty five
# 3 6

#How to use data with column priority
my $mat = {
  values ​​=> [1, 2, 3, 4, 5, 6],
  rows_length => 3,
  columns_length => 2,
};;

The calculation to find the difference in the matrix is ​​just the calculation to find the difference between each element of the matrix of the array.

Perl program to find the difference between matrices

A Perl program that finds the difference between matrices.

use strict;
use warnings;

# Matrix difference
sub mat_sub {
  my ($mat1, $mat2) = @_;
  
  my $mat_out = {};
  $mat_out->{rows_length} = $mat1->{rows_length};
  $mat_out->{columns_length} = $mat2->{columns_length};
  
  for (my $i = 0; $i <@{$mat1->{values}}; $i ++) {
    $mat_out->{values}->[$i] = $mat1->{values}->[$i]-$mat2->{values}->[$i];
  }
  
  return $mat_out;
}

my $mat1 = {
  values ​​=> [1, 2, 3, 4, 5, 6],
  rows_length => 3,
  columns_length => 2,
};;

my $mat2 = {
  values ​​=> [7, 8, 9, 10, 11, 12],
  rows_length => 3,
  columns_length => 2,
};;

# Matrix difference
my $mat_sub = mat_sub ($mat1, $mat2);

# [Matrix Subtract] Row: 3, Column: 2, Values: -6 -6 -6 -6 -6 -6
print "[Matrix Subtract] Row: $mat_sub->{rows_length}, Column: $mat_sub->{columns_length}, Values: @{$mat_sub->{values}}\n";

Associated Information