Open In App

JavaScript Program for Subtraction of Matrices

Last Updated : 12 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Subtraction of two matrices is a basic mathematical operation that is used to find the difference between the two matrices. In this article, we will see how we can perform the subtraction of input matrices using JavaScript.

Example:

Screenshot-2023-09-14-at-10-47-41-Browser

Using Loop in JavaScript

In this approach, we are using for loops to iterate over every individual element of the input matrices. This approach can perform the subtraction of any of the number of matrices, and then we are performing the subtraction of the response element and saving the result of the subtraction in the result matrix.

Syntax:

for (statement 1 ; statement 2 ; statement 3){
code here...
}

Example: In this example, we will be performing the Subtraction of Matrices in JavaScript by using Loops.

Javascript




function MatSubUSingForLoop(inputMat) {
    if (inputMat.length < 2) {
  
        // We are checking if length of
        // matrices are less than 2, as
  
        // We need atleat 2 matrices to
        // Perform substraction
        return null;
    }
    let outputMat = inputMat[0];
  
    // Loop fr Substraction of Matrices
    for (
        let i = 1;
        i < inputMat.length;
        i++) {
        let currMat = inputMat[i];
        if (
            outputMat.length !==
            currMat.length ||
            outputMat[0].length !==
            currMat[0].length) {
            console.log("Not Possible");
            return -1;
        }
        for (
            let j = 0;
            j < outputMat.length;
            j++) {
            for (
                let k = 0;
                k < outputMat[j].length;
                k++) {
                outputMat[j][k] -=
                    currMat[j][k];
            }}}
    return outputMat;
}
  
// Multiple Matrix Inputs
let inputMatrix1 = [
    [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4],
    [5, 5, 5, 5],
];
  
let inputMatrix2 = [
    [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4],
];
  
let inputMatrix3 = [
    [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4],
];
  
// Output Matrix
let resultMatrix = MatSubUSingForLoop([
    inputMatrix1,
    inputMatrix2,
    inputMatrix3,
]);
  
// Printing the result Matrix
for (
    let i = 0;
    i < resultMatrix.length;
    i++) {
    console.log(
        resultMatrix[i].join(" "));
}


Output

Not Possible

Using the map method in JavaScript

In this approach, we are using the inbuilt ‘map‘ method to create a new output matrix by substracting the input matrices. Here, the code can perform the substraction of any number of the matrices.

Syntax:

map((element, index, array) => { /* … */ })

Example: In this example, we will be performing the subtraction of matrices in JavaScript by using the map method.

Javascript




function matSubUsingMap(inputMat) {
    if (inputMat.length < 2) {
  
        // We are checking length of
        // matrices are less than 2
        return null;
    }
  
    // Temporary we are intializing
    // result matrix with 1st matrix
    let resultMatrix = inputMat[0];
    for (
        let i = 1;
        i < inputMat.length;
        i++) {
        let currMat = inputMat[i];
        if (
            resultMatrix.length !==
            currMat.length ||
            resultMatrix[0].length !==
            currMat[0].length) {
            console.log("Not Possible")
            return null;
        }
        resultMatrix = resultMatrix.map(
            function (rowMat, i) {
                return rowMat.map(
                    function (
                        valueMat, j) {
                        return (
                            valueMat -
                            currMat[i][j]);
                    });
            });
    }
    return resultMatrix;
}
  
// All the Input Matrices are given here
let inputMatrix1 = [
    [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4],
];
let inputMatrix2 = [
    [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4],
];
let inputMatrix3 = [
    [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4],
];
  
// We are printing the output
let resultMatrix = matSubUsingMap([
    inputMatrix1,
    inputMatrix2,
    inputMatrix3,
]);
for (
    let i = 0;
    i < resultMatrix.length;
    i++) {
    console.log(
        resultMatrix[i].join(" "));
}


Output

-1 -1 -1 -1
-2 -2 -2 -2
-3 -3 -3 -3
-4 -4 -4 -4

Using the Array.from() method

In this approach, we are using the Array.from() method in JavaScript. By using this function, we create a new matrix by iterating over each row and column of all the input matrices.

Syntax:

Array.from(object, mapFunction, thisValue)

Example: In this example, we will be performing the subtraction of matrices in JavaScript by using Array.from().

Javascript




function subtractMatricesUsingFrom(
    inputMat) {
    if (inputMat.length < 2) {
  
        // Checking length of matrix
        return null;
    }
    let resultMatrix = inputMat[0];
    for (
        let i = 1;
        i < inputMat.length;
        i++) {
        let currMat = inputMat[i];
  
        // Checking the dimensions here
        if (
            resultMatrix.length !==
            currMat.length ||
            resultMatrix[0].length !==
            currMat[0].length) {
            console.log("Not Possible");
            return null;
        }
  
        // Using from METHOD to iterate
        // over each row and column
        resultMatrix = Array.from(
            {
                length: resultMatrix.length,
            },
            (_, i) =>
                Array.from(
                    { length: resultMatrix[i].length, },
                    (_, j) =>
                        resultMatrix[i][j] - currMat[i][j]
                ));
    }
    return resultMatrix;
}
  
// All the input is defined here.
// multiple matrices
let inputMatrix1 = [
    [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4],
];
let inputMatrix2 = [
    [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4],
];
let inputMatrix3 = [
    [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4],
];
  
// Result is stored in this varibale
let resultMatrix =
    subtractMatricesUsingFrom([
        inputMatrix1,
        inputMatrix2,
        inputMatrix3,
    ]);
  
// Printing the results
resultMatrix.forEach((row) => {
    console.log(row.join(" "));
});


Output

-1 -1 -1 -1
-2 -2 -2 -2
-3 -3 -3 -3
-4 -4 -4 -4


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads