- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find matrix for which rows and columns holding sum of behind rows and columns in Python
Suppose we have a given matrix, We have to find a new matrix res, whose dimension is same as the given matrix where each element in res[i, j] = sum of the elements of matrix[r, c] for each r ≤ i, and c ≤ j.
So, if the input is like
then the output will be
To solve this, we will follow these steps −
if matrix is empty, then
R := row count of matrix
C := column count of matrix
for r in range 1 to R - 1, do
for c in range 0 to C - 1, do
matrix[r, c] := matrix[r, c] + matrix[r - 1, c]
for r in range 0 to R - 1, do
for c in range 1 to C - 1, do
matrix[r, c] := matrix[r, c] + matrix[r, c - 1]
Let us see the following implementation to get better understanding
def solve(matrix): if not matrix: return matrix R, C = len(matrix), len(matrix) for r in range(1, R): for c in range(C): matrix[r][c] += matrix[r - 1][c] for r in range(R): for c in range(1, C): matrix[r][c] += matrix[r][c - 1] return matrix matrix = [ [8, 2], [7, 4] ] print(solve(matrix))
[[8, 2],[7, 4]]
[[8, 10], [15, 21]]
- How to multiply a matrix columns and rows with the same matrix rows and columns in R?
- C program to sort all columns and rows of matrix
- How to find the sum of rows, columns, and total in a matrix in R?
- How to find the sum of rows and columns of a given matrix using Numpy?
- Finding index of rows and columns in SAP
- Python Pandas - Select a subset of rows and columns combined
- How to shuffle columns or rows of matrix in PyTorch?
- Finding the number of rows and columns in a given matrix using Numpy
- Flip Columns For Maximum Number of Equal Rows in Python
- Write a Python program to find the mean absolute deviation of rows and columns in a dataframe
- How to divide matrix rows by number of columns in R?
- Python program to sort matrix based upon sum of rows
- How to get rows and columns of 2D array in Java?
- Count rows/columns with sum equals to diagonal sum in C++
- Is there a MAX function for rows and not for columns in MySQL?