Open In App

Addition of Two Matrices in PHP

Last Updated : 16 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two matrices mat1 and mat2, the task is to find the sum of both matrices. Matrix addition is possible only when both matrix’s dimensions are the same. If both matrices have different dimensions then addition will not be possible.

Sum of Two Matrices using for Loop

First, we declare two matrices, matrix1 and matrix2. Next, we check both matrix dimension, if both matrix dimension is same then we proceed for sum of matrices, otherwise display an error message. To add both matrices, we use for loop. For loop iterate each matrix elements one by one, and perform sum on the elements.

 

Example: Sum of two matrices using for Loop in PHP.

PHP




<?php
  
function sumTwoMatrices($mat1, $mat2) {
    $rows = count($mat1);
    $columns = count($mat1[0]);
  
    // Sum Matrix Initialize with Zeros
    $sum = array();
    for ($i = 0; $i < $rows; $i++) {
        for ($j = 0; $j < $columns; $j++) {
            $sum[$i][$j] = 0;
        }
    }
  
    // Matrix Addition
    for ($i = 0; $i < $rows; $i++) {
        for ($j = 0; $j < $columns; $j++) {
            $sum[$i][$j] = $mat1[$i][$j] + $mat2[$i][$j];
        }
    }
  
    return $sum;
}
  
// First Matrix
$matrix1 = array(
    array(26, 14, 31),
    array(41, 61, 11),
    array(14, 12, 19)
);
  
// Second Matrix
$matrix2 = array(
    array(11, 8, 27),
    array(21, 15, 25),
    array(22, 15, 21)
);
  
// Get the first Matrix dimension
$rows1 = count($matrix1);
$columns1 = count($matrix1[0]);
  
// Get the second Matrix dimension
$rows2 = count($matrix2);
$columns2 = count($matrix2[0]);
  
// Check both matrix dimension is equal or not
if($rows1 != $rows2 || $columns1 != $columns2) {
    echo "Matrix dimension not Equal. Sum not possible";
}
else {
      
    // Sum of Matrices
    $sum = sumTwoMatrices($matrix1, $matrix2);
  
    // Print the Sum Matrix
    foreach ($sum as $row) {
        echo implode("\t", $row) . "\n";
    }
}
  
?>


Output

37    22    58
62    76    36
36    27    40


Similar Reads

Php Program to multiply two matrices
Given two matrices, the task to multiply them. Matrices can either be square or rectangular. Examples:  Input : mat1[][] = {{1, 2}, {3, 4}} mat2[][] = {{1, 1}, {1, 1}} Output : {{3, 3}, {7, 7}} Input : mat1[][] = {{2, 4}, {3, 4}} mat2[][] = {{1, 2}, {1, 3}} Output : {{6, 16}, {7, 18}}Recommended: Please solve it on "PRACTICE" first, before moving o
3 min read
How to Find Multiplication of Two Matrices of any Size in PHP?
Multiplying two matrices is a common operation in linear algebra. To multiply two matrices of any size in PHP, you can create a function that takes two matrices as input and returns their product. Below are the approaches to find the Multiplication of two Matrices of any size in PHP: Table of Content Using nested loopsUsing the array_mapUsing neste
3 min read
Addition of Two Fractions using JavaScript
Fraction numbers in math are representations of parts of a whole, consisting of a numerator (the part being considered) and a denominator (the total number of equal parts). Add two fractions a/b and c/d and print the answer in simplest form. Examples: Input: 1/2 + 3/2Output: 2/1Input: 1/3 + 3/9Output: 2/3Table of Content Using Math OperationsUsing
3 min read
JavaScriptProgram to Check Two Matrices are Identical or Not
We have given two matrices with the data as the input and the task is to check whether these input matrices are identical/same or not. When the number of rows and columns and the data elements are the same, the two matrices are identical. Below is the Example for better understanding: Example: So to check whether the input matrices are identical or
4 min read
JavaScript Program to Add Two Matrices
Given two N x M matrices. Find a N x M matrix as the sum of given matrices each value at the sum of values of corresponding elements of the given two matrices In this article, we will see how we can perform the addition of two input matrices using JavaScript. Example: Table of ContentUsing Loop in JavaScriptUsing map methodUsing the Array.from() me
3 min read
Javascript Program to multiply two matrices
Given two matrices, the task to multiply them. Matrices can either be square or rectangular. Examples:  Input : mat1[][] = {{1, 2}, {3, 4}} mat2[][] = {{1, 1}, {1, 1}} Output : {{3, 3}, {7, 7}} Input : mat1[][] = {{2, 4}, {3, 4}} mat2[][] = {{1, 2}, {1, 3}} Output : {{6, 16}, {7, 18}}Recommended: Please solve it on "PRACTICE" first, before moving o
3 min read
Javascript Program for Kronecker Product of two matrices
Given a [Tex]{m} imes{n} [/Tex]matrix A and a [Tex]{p} imes{q} [/Tex]matrix B, their Kronecker product C = A tensor B, also called their matrix direct product, is an [Tex]{(mp)} imes{(nq)} [/Tex]matrix. A tensor B = |a11B a12B| |a21B a22B| = |a11b11 a11b12 a12b11 a12b12| |a11b21 a11b22 a12b21 a12b22| |a11b31 a11b32 a12b31 a12b32| |a21b11 a21b12 a22
3 min read
Addition(+) Arithmetic Operator in JavaScript
JavaScript arithmetic addition operator is capable of performing two types of operation that is addition and concatenation. It is used to perform some on the number or concatenate Strings. Syntax: a+bReturn Type: It returns the sum/concatenation of operators. Example 1: In this example, we will use the addition operator on some primitive data types
1 min read
Addition Assignment (+=) Operator in Javascript
JavaScript Addition assignment operator(+=) adds a value to a variable, The Addition Assignment (+ =) Sums up left and right operand values and then assigns the result to the left operand. The two major operations that can be performed using this operator are the addition of numbers and the concatenation of strings. Syntax: a += b Example 1: In thi
2 min read
Segment Tree for Range Addition and Range Minimum Query
Given an array of size N initially filled with 0s. There are Q queries to be performed, and each query can be one of two types: Type 1 (1, L, R, X): Add X to all elements in the segment from L to R−1.Type 2 (2, L, R): Find the minimum value in the segment from L to R−1.Example: Input: n = 5, q = 6, queries = { {1, 0, 3, 3}, {2, 1, 2}, {1, 1, 4, 4},
16 min read