JavaScript MatrixWe will study the JavaScript matrix in this article. Let us first comprehend about array and matrix. We all know how to create a dimensional array but most of us do not know how to create a 2-dimensional array so in this article we will learn how to construct an array of 2-dimensional in JavaScript. ArrayAn array is a variable that can hold more than one value of the same type. The array can be one-dimensional, two-dimensional, three-dimensional, etc. Example: The above array named "cricketers" is a one-dimensional array that contains the name of cricketers only which means an array can hold the value of the same type of data. Constructing an array in JavaScript:We can create an array in JavaScript by utilizing array literal. Syntax: In the above-provided syntax, the "var" is the keyword that is utilized to create an array that can be initialized at any time. The "arrayName" is the name of the array. The element1, element2 and element3 are the same kind of elements present in the array. MatrixA matrix is a rectangular array that consists of rows and columns. It is similar to a table or grid that contains rows and columns. Dimensions of matrixFollowing are the examples that show the dimensions of a matrix: Example-1: The above matrix contains one row and two columns which means this matrix has a dimension of (1 x 2). Example-2: The above matrix contains two rows and three columns which means this matrix has a dimension of (2 x 3). Matrix in JavaScriptIn JavaScript, the matrix is also called an array of arrays or multidimensional array. Operations of MatrixLet us first consider the following matrix on which we will perform all operations. Following are the operations that are performed by the matrix: 1. Row: It is utilized to return the row.
It returns [9, 1, 5]. 2. Column: It is utilized to return the column.
It returns [[9], [3], [4]]. 3. Identity:
It returns [[9, 1, 5], [3, 2, 5], [4, 8, 6]]. 4. Range:
It returns [[3, 2, 5], [4, 8, 6]]. 5. Set:
It returns [[0, 0, 0], [3, 2, 5], [4, 8, 6]]. 6. Size: It is utilized to find the size of the matrix.
It returns [3, 3]. 7. Addition: It is utilized to add two matrices.
It adds matrix B and matrix C. 8. Subtraction: It is utilized to subtract one matrix from another.
It subtracts matrix B from matrix C. 9. Multiplication: It is utilized to multiply two matrices.
It multiplies matrices B and C. 10. Division: It is utilized to divide one matrix by another.
It divides matrix B by matrix C. 11. Product: It is utilized to calculate the product of two matrices.
It returns the product of matrix B and matrix C. 12. Transpose: It is utilized to find the transpose of a matrix.
It transposes the matrix B. 13. Inverse: It is utilized to inverse a matrix.
It inverses the matrix C. 14. Determinant: It is utilized to calculate the determinant of a matrix.
It returns the determinant of B. 15. Merge: Two matrices can be merged in the top, bottom, left and right directions. Top direction: It merges the matrix in the top direction. Bottom direction: It merges the matrix in the bottom direction. Left direction: It merges the matrix in the left direction. Right direction: It merges the matrix in the right direction. 16. Equals: It is utilized to check whether the two matrices are equal or not. Demonstrations of JavaScript MatrixLet us see demonstrations for some of the matrix operations in JavaScript. Demonstration-1:We will construct a program for adding matrix A and matrix B. Code: Output: We can witness a resultant matrix which is the addition of matrix A and matrix B. Demonstration-2:We will build up a program to subtract matrix B from matrix A. Code: Output: We can witness a resultant matrix which is the subtraction of matrix B from matrix A. Demonstration-3:We will construct a program to multiply matrix A and matrix B. Code: Output: Here in the outcome we can witness a resultant matrix which is the multiplication of matrix A and matrix B. Demonstration-4:We will construct a JavaScript program to divide matrix C by matrix D. Code: Output: Here is the outcome in which we can have a gaze at the resultant matrix which is the division of matrix C by matrix D. Conclusion:We have understood the JavaScript Matrix in this article. A matrix is a two-dimensional array that contains rows and columns. There are many operations that can be performed on matrices and we have discussed some of the operations with the assistance of demonstrations. Next TopicgroupBy() in JavaScript |