
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Sum of Columns of Matrix Elements in Swift
A matrix is an arrangement of numbers in rows and columns. The matrix can be of various types like square, horizontal, vertical, etc.
In this article, we will learn how to write a swift program to calculate the sum of columns of matrix elements. Here we are using the following methods to find the sum of the column's elements ?
Using a nested for-in loop
Using in-built function
Method 1: Using nested for-in loop
Here we use a nested for-in loop to find the sum of columns of matrix elements.
Algorithm
Step 1 ? Create a function.
Step 2 ? Create a variable named sum to store the sum. The initial value of sum = 0.
Step 3 ? Find the number of rows and columns.
Step 4 ? Run nested for-in loop to iterate through each row and column.
Step 5 ? In this nested loop, add elements column-wise and store the result into sum variable.
Step 6 ? After each column reset the value of sum = 0 for the sum of next column elements.
Step 7 ? Create a matrix and pass it in the function.
Step 8 ? Print the output.
Example
Following Swift program to calculate the sum of column of matrix elements.
import Foundation import Glibc // Function to find the sum of each column of a matrix func columnElementsSum(matrix: [[Int]]) { var sum = 0 let noRow = matrix.count let noColumn = matrix[0].count for C in 0..<noColumn { for R in 0..<noRow { sum += matrix[R][C] } print("Sum of Column \(C) = \(sum)") sum = 0 } } // Input 4x5 matrix let M = [[1, 2, 2, 5, 2], [1, 1, 6, 3, 6], [7, 8, 3, 0, 1], [2, 4, 2, 7, 1]] print("Matrix is:") for x in 0..<M.count { for y in 0..<M[0].count { print(M[x][y], terminator:" ") } print("\n") } // Calling function columnElementsSum(matrix: M)
Output
Matrix is: 1 2 2 5 2 1 1 6 3 6 7 8 3 0 1 2 4 2 7 1 Sum of Column 0 = 11 Sum of Column 1 = 15 Sum of Column 2 = 13 Sum of Column 3 = 15 Sum of Column 4 = 10
Here in the above code, we have a 4x5 matrix and then pass it in the function named columnElementsSum() to find the sum of the column's elements of the matrix. In this function, we use nested for loop to iterate through each element of the input matrix and then add it to the corresponding column sum. After finishing the nested for loop this function will print the individual columns sum that is column 0 = 11, column 1 = 15, column 2 = 13 and so on(according to the input matrix).
Method 2: Using in-built Function
To find the sum of the rows of the given matrix elements we uses reduce(_:_:) function. This function returns a result by combining the elements of the array or any sequence using the given closure.
Syntax
func reduce(_initial: Value, _next: Value)
Here, initial represent the value to use as an initial accumulating value. It passes to the next for the first time the closure is executed. And next represent a closure that combines an accumulating value and an element of the array into a new accumulating value which if further used for next call of the next closure or return to the caller.
Algorithm
Step 1 ? Create a function.
Step 2 ? Find the count of columns.
Step 3 ? Create an 1-D array to store the sum of the columns.
Step 4 ? Run a for loop to iterate through each column.
Step 5 ? Find the sum of the columns elements using reduce function.
Step 6 ? Now store the sum of each row in the array using + operator function.
Step 7 ? Return the resultant array.
Step 8 ? Create a matrix and pass it in the function.
Step 9 ? Print the output.
Example
Following Swift program to calculate the sum of rows of matrix elements.
import Foundation import Glibc // Function to find sum of columns of matrix elements. func ColumnSum(matrix: [[Int]]) -> [Int] { let col = matrix[0].count var colSum = Array(repeating: 0, count: col) for C in 0..<col { colSum[C] = matrix.reduce(0) { (res, row) -> Int in return res + row[C] } } return colSum } // 4x4 matrix let M = [[1, 2, 3, 1], [4, 5, 6, 3], [7, 8, 9, 5], [3, 3, 4, 5]] // Displaying matrix print("Matrix is:", M) let output = ColumnSum(matrix: M) print("Sum of the columns are:", output)
Output
Matrix is: [[1, 2, 3, 1], [4, 5, 6, 3], [7, 8, 9, 5], [3, 3, 4, 5]] Sum of the columns are: [15, 18, 22, 14]
Here in the above code, we have a 4x4 matrix. Then create function which takes array as an argument and return an array which contains the sum of the sum of each columns. In this function, create an empty 1-D array to store the sum of each row and find the column elements count. Then the outer for-in loop iterates through each column and for each column the reduce() function iterates through each row of the metric and adds the current element of the matrix to the corresponding column sum. So according to the given matrix the final result is [15, 18, 22, 14]. Here 15 is the sum of row 0 elements(1+4+7+3), 18 is the sum of row 1 elements(2+5+8+3) and so on.
Conclusion
So this is how we can calculate the sum of columns of matrix elements. Here using for-in loop we can calculate the sum of any type of matrix like 3x3, 4x5, 7x3, etc. Whereas using method 2 we can calculate the sum of only square matrix(where number of rows is equal to number of columns).