A matrix is a two-dimensional arrangement of elements (typically numbers) organized in rows and columns. In computer science, matrices are fundamental data structures used in various applications, from image processing to graph algorithms.
class Matrix {private: vector<vector<int>> matrix; int rows, cols;public: Matrix(int r, int c) : rows(r), cols(c) { matrix.resize(rows, vector<int>(cols, 0)); } void setElement(int i, int j, int value) { if (i >= 0 && i < rows && j >= 0 && j < cols) matrix[i][j] = value; else throw out_of_range("Matrix indices out of range"); } int getElement(int i, int j) { if (i >= 0 && i < rows && j >= 0 && j < cols) return matrix[i][j]; throw out_of_range("Matrix indices out of range"); }};
class Matrix: def __init__(self, rows, cols): self.rows = rows self.cols = cols self.matrix = [[0 for j in range(cols)] for i in range(rows)] def set_element(self, i, j, value): if 0 <= i < self.rows and 0 <= j < self.cols: self.matrix[i][j] = value else: raise IndexError("Matrix indices out of range") def get_element(self, i, j): if 0 <= i < self.rows and 0 <= j < self.cols: return self.matrix[i][j] raise IndexError("Matrix indices out of range")
class Matrix { constructor(rows, cols) { this.rows = rows; this.cols = cols; this.matrix = Array(rows).fill().map(() => Array(cols).fill(0)); } setElement(i, j, value) { if (i >= 0 && i < this.rows && j >= 0 && j < this.cols) { this.matrix[i][j] = value; } else { throw new Error("Matrix indices out of range"); } } getElement(i, j) { if (i >= 0 && i < this.rows && j >= 0 && j < this.cols) { return this.matrix[i][j]; } throw new Error("Matrix indices out of range"); }}
// Matrix Additionvector<vector<int>> addMatrices(const vector<vector<int>>& A, const vector<vector<int>>& B) { int rows = A.size(), cols = A[0].size(); vector<vector<int>> C(rows, vector<int>(cols)); for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) C[i][j] = A[i][j] + B[i][j]; return C;}// Matrix Multiplicationvector<vector<int>> multiplyMatrices(const vector<vector<int>>& A, const vector<vector<int>>& B) { int m = A.size(), n = A[0].size(), p = B[0].size(); vector<vector<int>> C(m, vector<int>(p, 0)); for(int i = 0; i < m; i++) for(int j = 0; j < p; j++) for(int k = 0; k < n; k++) C[i][j] += A[i][k] * B[k][j]; return C;}
def add_matrices(A, B): rows, cols = len(A), len(A[0]) C = [[0 for j in range(cols)] for i in range(rows)] for i in range(rows): for j in range(cols): C[i][j] = A[i][j] + B[i][j] return Cdef multiply_matrices(A, B): m, n, p = len(A), len(A[0]), len(B[0]) C = [[0 for j in range(p)] for i in range(m)] for i in range(m): for j in range(p): for k in range(n): C[i][j] += A[i][k] * B[k][j] return C
function addMatrices(A, B) { const rows = A.length, cols = A[0].length; const C = Array(rows).fill() .map(() => Array(cols).fill(0)); for(let i = 0; i < rows; i++) for(let j = 0; j < cols; j++) C[i][j] = A[i][j] + B[i][j]; return C;}function multiplyMatrices(A, B) { const m = A.length, n = A[0].length, p = B[0].length; const C = Array(m).fill() .map(() => Array(p).fill(0)); for(let i = 0; i < m; i++) for(let j = 0; j < p; j++) for(let k = 0; k < n; k++) C[i][j] += A[i][k] * B[k][j]; return C;}