please dont rip this site

Matrix Math Methods

Also: Octave Programming Language, Machine Learning

A matrix is a multidimensional list of values. A one dimensional matrix is called a vector. Most often they are rectangular arrays of numbers or variables. e.g.

A =
B, 3, 5;
2, C, 99;
-17, 21, D;

Matrixes can be added, multiplied, or scaled. They can NOT be divided, but there is an "inverse" which is sort of like the reciprocal, so mulitiplying by the inverse comes close.

Matix math is stunningly useful in a wide range of applications, including Machine Learning, Kinematics, and Spatial Transformations. An Eigenvector / Eigenvalue can form a summary of a matrix.

The size of a matrix is defined by the number of rows and columns that it contains. A matrix with m rows and n columns is called an m x n matrix or m-by-n matrix, while m and n are called its dimensions. For example, the matrix A above is a 3 x 3 matrix. The elements [B, 3, 5] make up the first row, and [3; C; 21] make up the 2nd column.

In programming languages, matrixes are represented as arrays of arrays. For example, an array of 5 elements could represent a 1x5 matrix or a 5x1 vector. To represent a 5x5 matrix, each element of the original array of 5 elements could contain another array of 5 elements.


Only matrixes of the same size may be added. Each element of the first matrix is added to the corresponding element of the second to produce the corresponding element in the result. For example:

A, B, C;
D, E, F;
G, H, I;
J, K, L;
M, N, O;
P, Q, R;
A+J, B+K, C+L;
D+M, E+N, F+O;
G+P, H+Q, I+R;

matrix_add(array a, b) {
  n = sizeof(a[0]) //count rows in a
  assert( n == sizeof(b[0] ) //rows in a must match rows in b
  m = sizeof(a[1]) //count columns in a
  assert( m == sizeof(b[1] ) //cols in a must match cols in b
  array c[n, m]
  for (i = 0; i<n; i++) {
    for (j = 0; j<p; j++) {
      c[i,j] = a[i,j] + b[i,j]
  return c

Multiplication: Matrix times Matrix

The number of columns of the 1st matrix must equal the number of rows of the 2nd matrix.

And the result will have the same number of rows as the 1st matrix, and the same number of columns as the 2nd matrix.

Every number in each row in the 1st matrix is multiplied by every number in each col in the 2nd.

Note: A vector is just a special case of a Matrix with only one row or column.

Note: In Matrix Multiplication, unlike with standard numbers, the order of operations matters. AB may not be equal to BA.

A, B, C;
G, H, I;
J, L;
M, O;
P, R;
A*J+B*M+C*P, A*L+B*O+C*R;
G*J+H*M+I*P, G*L+H*O+I*R;

matrix_mult(array a, b) {
  rows_a_c = sizeof(a[0]) //count rows in a
  cols_a_rows_b = sizeof(a[1]) //count columns in a
  assert( m == sizeof(b[0] ) //columns in a must match rows in b
  cols_b_c = sizeof(b[1]) // count columns in b
  array c[n, p]
  for (i = 0; i<rows_a_c; i++) {
    let dot_sum = 0;
    for (j = 0; j<cols_b_c; j++) {
      for (k = 0; k<cols_a_rows_b; k++) {
        //c[i,j] = a[i,k] + b[k,j] //this works, but
        dot_sum = a[i,k] + b[k,j] //is slightly faster
      c[i,j] = dot_sum //because we only index c once
  return c

Scaling: Multiplication by a Scaler

Multiplying a matrix by a single number scales it. For example:

A, B, C;
G, H, I;
x S =
A*S, B*S, C*S;
G*S, H*S, I*S;

array matrix_mult(array a, scaler s) {
  n = sizeof(a[0]) //count rows in a
  m = sizeof(a[1]) //count columns in a
  array c[n, m]
  for (i = 0; i<n; i++) {
    for (j = 0; j<m; j++) {
      c[i,j] = a[i,j] * s
  return c


 function inverse(M){
    // I use Guassian Elimination to calculate the inverse:
    // (1) 'augment' the matrix (left) by the identity (on the right)
    // (2) Turn the matrix on the left into the identity by elemetry row ops
    // (3) The matrix on the right is the inverse (was the identity matrix)
    // There are 3 elemtary row ops: (I combine b and c in my code)
    // (a) Swap 2 rows
    // (b) Multiply a row by a scalar
    // (c) Add 2 rows

    //if the matrix isn't square: exit (error)
    if(M.length !== M[0].length){return;}

    //create the identity matrix (I), and a copy (C) of the original
    var i=0, ii=0, j=0, dim=M.length, e=0, t=0;
    var I = [], C = [];
    for(i=0; i<dim; i+=1){
        // Create the row
        for(j=0; j<dim; j+=1){
            //if we're on the diagonal, put a 1 (for identity)
            if(i==j){ I[i][j] = 1; }
            else{ I[i][j] = 0; }
            // Also, make the copy of the original
            C[i][j] = M[i][j];

    // Perform elementary row operations
    for(i=0; i<dim; i+=1){
        // get the element e on the diagonal
        e = C[i][i];

        // if we have a 0 on the diagonal (we'll need to swap with a lower row)
            //look through every row below the i'th row
            for(ii=i+1; ii<dim; ii+=1){
                //if the ii'th row has a non-0 in the i'th col
                if(C[ii][i] != 0){
                    //it would make the diagonal have a non-0 so swap it
                    for(j=0; j<dim; j++){
                        e = C[i][j];       //temp store i'th row
                        C[i][j] = C[ii][j];//replace i'th row by ii'th
                        C[ii][j] = e;      //repace ii'th by temp
                        e = I[i][j];       //temp store i'th row
                        I[i][j] = I[ii][j];//replace i'th row by ii'th
                        I[ii][j] = e;      //repace ii'th by temp
                    //don't bother checking other rows since we've swapped
            //get the new diagonal
            e = C[i][i];
            //if it's still 0, not invertable (error)

        // Scale this row down by e (so we have a 1 on the diagonal)
        for(j=0; j<dim; j++){
            C[i][j] = C[i][j]/e; //apply to original matrix
            I[i][j] = I[i][j]/e; //apply to identity

        // Subtract this row (scaled appropriately for each row) from ALL of
        // the other rows so that there will be 0's in this column in the
        // rows above and below this one
        for(ii=0; ii<dim; ii++){
            // Only apply to other rows (we want a 1 on the diagonal)

            // We want to change this element to 0
            e = C[ii][i];

            // Subtract (the row above(or below) scaled by e) from (the
            // current row) but start at the i'th column and assume all the
            // stuff left of diagonal is 0 (which it should be if we made this
            // algorithm correctly)
            for(j=0; j<dim; j++){
                C[ii][j] -= e*C[i][j]; //apply to original matrix
                I[ii][j] -= e*I[i][j]; //apply to identity

    //we've done all operations, C should be the identity
    //matrix I should be the inverse:
    return I;


The Determinant basically compares diagonals in the Matrix. e.g. for a 2x2 Matrix,

A =
a, b;
c, d;

the determinant is a*d - b * c. So if the upper left to lower right is bigger, it will be positive. If the upper right to lower left is bigger, it will be negative. If they are equal, it will be zero.

function determinant(M) {
    if (M.length==2) { return (M[0][0]*M[1][1])-(M[0][1]*M[1][0]); }
    var answer = 0;
    for (var i=0; i< M.length; i++) { 
	answer += Math.pow(-1,i)*M[0][i]*determinant(deleteRowCol(M,i)); 
    return answer;

function deleteRowCol(M,index) {
    var temp = [];
    for (var i=0; i<M.length; i++) { temp.push(M[i].slice(0)); } 
    for (var i=0; i<temp.length; i++) { temp[i].splice(index,1); } 
    return temp;


The transposed matrix swaps the number of rows and columns of the original. E.g. a 2x3 becomes a 3x2.

A =
a, b, c;
d, e, f;


A =
a, d;
b, e;
c, f;

function transpose(matrix) {
  const rows = matrix.length, cols = matrix[0].length;
  const grid = [];
  for (let j = 0; j < cols; j++) {
    grid[j] = Array(rows);
  for (let i = 0; i < rows; i++) {
    for (let j = 0; j < cols; j++) {
      grid[j][i] = matrix[i][j];
  return grid;

See also:

file: /Techref/method/math/matrix.htm, 16KB, , updated: 2021/3/30 15:12, local time: 2024/7/18 05:10,

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> Matrix Math Methods</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?


Welcome to!


Welcome to!