C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to calculate the subtraction of 2 matrices
ExplanationIn this program, we need to subtract two matrices and print the resulting matrix. Subtraction of two matrices:Matrix B can be subtracted from matrix A or vice versa if and only if they have same dimensions that are, the same number of rows and columns. It is not possible to subtract a 2 � 3 matrix from a 3 � 2 matrix. Subtraction of two matrices can be performed by subtracting their corresponding elements as (A - B)ij = Aij - Bij
Subtraction of two matrices can be performed by looping through the first and second matrix. Calculating the difference between their corresponding elements and store the result in the third matrix. Algorithm
SolutionPython
#Initialize matrix a
a = [
[4, 5, 6],
[3, 4, 1],
[1, 2, 3]
];
#Initialize matrix b
b = [
[2, 0, 3],
[2, 3, 1],
[1, 1, 1]
];
#Calculates number of rows and columns present in given matrix
rows = len(a);
cols = len(a[0]);
#Array diff will hold the result and is initialized with zeroes.
diff = [[0]*rows for i in range(cols)];
#Performs subtraction of matrices a and b. Store the result in matrix diff
for i in range(0, rows):
for j in range(0, cols):
diff[i][j] = a[i][j] - b[i][j];
print("Subtraction of two matrices: ");
for i in range(0, rows):
for j in range(0, cols):
print(diff[i][j]),
print(" ");
Output: Subtraction of two matrices: 2 5 3 1 1 0 0 1 2 C
#include <stdio.h>
int main()
{
int rows, cols;
//Initialize matrix a
int a[][3] = {
{4, 5, 6},
{3, 4, 1},
{1, 2, 3}
};
//Initialize matrix b
int b[][3] = {
{2, 0, 3},
{2, 3, 1},
{1, 1, 1}
};
//Calculates number of rows and columns present in given matrix
rows = (sizeof(a)/sizeof(a[0]));
cols = (sizeof(a)/sizeof(a[0][0]))/rows;
//Array diff will hold the result
int diff[rows][cols];
//Performs subtraction of matrices a and b. Store the result in matrix diff
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
diff[i][j] = a[i][j] - b[i][j];
}
}
printf("Subtraction of two matrices: \n");
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
printf("%d ", diff[i][j]);
}
printf("\n");
}
return 0;
}
Output: Subtraction of two matrices: 2 5 3 1 1 0 0 1 2 JAVA
public class SubMatrix
{
public static void main(String[] args) {
int rows, cols;
//Initialize matrix a
int a[][] = {
{4, 5, 6},
{3, 4, 1},
{1, 2, 3}
};
//Initialize matrix b
int b[][] = {
{2, 0, 3},
{2, 3, 1},
{1, 1, 1}
};
//Calculates number of rows and columns present in given matrix
rows = a.length;
cols = a[0].length;
//Array diff will hold the result
int diff[][] = new int[rows][cols];
//Performs subtraction of matrices a and b. Store the result in matrix diff
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
diff[i][j] = a[i][j] - b[i][j];
}
}
System.out.println("Subtraction of two matrices: ");
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
System.out.print(diff[i][j] + " ");
}
System.out.println();
}
}
}
Output: Subtraction of two matrices: 2 5 3 1 1 0 0 1 2 C#
using System;
public class SubMatrix
{
public static void Main()
{
int rows, cols;
//Initialize matrix a
int[,] a = {
{4, 5, 6},
{3, 4, 1},
{1, 2, 3}
};
//Initialize matrix b
int[,] b = {
{2, 0, 3},
{2, 3, 1},
{1, 1, 1}
};
//Calculates number of rows and columns present in given matrix
rows = a.GetLength(0);
cols = a.GetLength(1);
//Array diff will hold the result
int[,] diff = new int[rows, cols];
//Performs subtraction of matrices a and b. Store the result in matrix diff
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
diff[i,j] = a[i,j] - b[i,j];
}
}
Console.WriteLine("Subtraction of two matrices: ");
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
Console.Write(diff[i,j] + " ");
}
Console.WriteLine();
}
}
}
Output: Subtraction of two matrices: 2 5 3 1 1 0 0 1 2 PHP
<!DOCTYPE html>
<html>
<body>
<?php
//Initialize matrix a
$a = array(
array(4, 5, 6),
array(3, 4, 1),
array(1, 2, 3)
);
//Initialize matrix b
$b = array(
array(2, 0, 3),
array(2, 3, 1),
array(1, 1, 1)
);
//Calculates number of rows and columns present in given matrix
$rows = count($a);
$cols = count($a[0]);
//Array diff will hold the result and initialize it with 0
$diff = array_fill(0, $cols, array_fill(0, $rows, 0));
//Performs subtraction of matrices a and b. Store the result in matrix diff
for($i = 0; $i < $rows; $i++){
for($j = 0; $j < $cols; $j++){
$diff[$i][$j] = $a[$i][$j] - $b[$i][$j];
}
}
print("Subtraction of two matrices: <br>");
for($i = 0; $i < $rows; $i++){
for($j = 0; $j < $cols; $j++){
print($diff[$i][$j] . " ");
}
print("<br>");
}
?>
</body>
</html>
Output: Subtraction of two matrices: 2 5 3 1 1 0 0 1 2
Next Topic#
|