Given a list (length = n) of 2x2 matrices, how do I calculate the sum of all those matrices (and get a 2x2 matrix) ?
How can I do it, if instead of a list I have those matrices in a (2 x 2 x n) dimensional array ?
The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int x[10][20] can store total (10*20) = 200 elements. Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.
You can find the sum of all elements in an array by following the approach below: Initialize a variable sum to store the total sum of all elements of the array. Traverse the array and add each element of the array with the sum variable. Finally, return the sum variable.
The sum of each element of the 2D array can be calculated by traversing through the matrix and adding up the elements.
It's a += b ( a = a + b ), not a =+b (see your second loop).
Sum of matrices in a list:
Reduce("+", matrix_list)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With