# Swift Program to Subtract Two Matrix Using Multi-dimensional Arrays

In this article, we will learn how to write a swift program to subtract two matrices using multi-dimensional arrays.

A matrix is a mathematical structure in which the elements are placed in rows and columns format. For example, the first element is present at a00 location, the second at a01, and so on. So to subtract two matrices we are going to use the - operator to subtract the elements of two matrices like a00 - b00 and then store the sum into a new matrix. For example −

Matrix 1 −

$\mathrm{\begin{bmatrix}1 & 8 & 4 \newline8 & 8 & 7 \newline9 & 9 & 3\end{bmatrix}}$

Matrix 2 −

$\mathrm{\begin{bmatrix}2 & 7 & 3 \newline1 & 9 & 4 \newline9 & 3 & 2\end{bmatrix}}$

So subtract = Matrix 1 - Matrix 2

$\mathrm{Subtract\:=\:\begin{bmatrix}1\:-\:2 & 8\:-\:7 & 4\:-\:3 \newline8\:-\:1 & 8\:-\:9 & 7\:-\:4 \newline9\:-\:9 & 9\:-\:3 & 3\:-\:2\end{bmatrix}\:=\:\begin{bmatrix}-1 & 1 & 1 \newline7 & -1 & 3 \newline0 & 6 & 1\end{bmatrix}}$

## Algorithm

Step 1 − Define the size of the matrix.

Step 2 − Create a function to find subtraction.

Step 3 − Create a matrix to store the difference. Here, the size and type of the matrix should be same are the input matrices.

Step 4 − Run nested for loop to iterate through each rows and columns of the given matrices and subtract matrix 1 from matrix 2 and store the result into sub matrix.

Step 5 − Create two matrices of same type.

Step 6 − Call the function and pass these matrices as a parameter in it to find the difference.

Step 7 − Print the output.

## Example

Following the Swift program to subtract two matrices using multidimensional arrays.

import Foundation
import Glibc

// Size of the matrix
var row = 5
var col = 5

// Function to find the difference between two matrices
func difference(mxt1:[[Int]], mxt2:[[Int]]){

// Creating 5x5 matrix to store the sum
var sub = Array(repeating: Array(repeating: 0, count: 5), count: 5)

// Find the difference of two matrices

// Using - operator
for x in 0..<row {
for y in 0..<col {
sub[x][y] = mxt1[x][y] - mxt2[x][y]
}
}
print("Resultant matrix:")
for x in 0..<row {
for y in 0..<col {
print(sub[x][y], terminator:" ")
}
print("\n")
}
}

// Creating 5x5 matrix of integer type
var matrix1 : [[Int]] = [[8, 3, 4, 5, 9], [9, 6, 7, 5, 7], [9, 5, 5, 9, 4], [8, 4, 9, 2, 4], [5, 9, 8, 3, 9]]
print("Matrix 1:")
for x in 0..<row {
for y in 0..<col {
print(matrix1[x][y], terminator:" ")
}
print("\n")
}
var matrix2 : [[Int]] = [[3, 3, 0, 1, 2], [2, 4, 7, 8, 7],  [1, 1, 1, 1, 4], [1, 4, 3, 3, 4], [5, 2, 2, 2, 5]]
print("Matrix 2:")
for x in 0..<row {
for y in 0..<col {
print(matrix2[x][y], terminator:" ")
}
print("\n")
}
difference(mxt1:matrix1, mxt2:matrix2)


## Output

Matrix 1:
8 3 4 5 9
9 6 7 5 7
9 5 5 9 4
8 4 9 2 4
5 9 8 3 9

Matrix 2:
3 3 0 1 2
2 4 7 8 7
1 1 1 1 4
1 4 3 3 4
5 2 2 2 5

Resultant matrix:
5 0 4  4  7
7 2 0 -3  0
8 4 4  8  0
7 0 6 -1  0
0 7 6  1  4


Here in the above code, we have two matrices of integer type. Now we create a function to find the difference between the given two matrices. So for the difference, we subtract the elements of matrix 1 from matrix 2 of the same positions and store the result into a new matrix at the same position on which they are subtracted like ADD[0][0] = mxt1[0][0]- mxt1[0][0], ADD[0][1] = mxt1[0][1]-mxt1[0][1], and so on using - operator.

## Conclusion

Therefore, this is how we can create and subtract two matrices using a multi-dimensional array. You can also create any size of matrices like 4x4, 6x3, and 2x3 using a multi-dimensional array and can able to perform operations on them.

Updated on: 09-Jan-2023

187 Views