Matrix Nodes

john's Avatar

john

19 Jul, 2026 10:15 PM

Attached are a DOZEN new nodes which all rely on a new Nodebox data type: matrix.

A matrix is an ordinary Nodebox table with the following constraints:

  • The first column is named "row" and contains distinct row headers
  • The first row contains distinct column headers (as do all Nodebox tables)
  • The values are numbers (floats or integers)

So, if you import matrix values from a CSV file, you will need to ensure it has proper column headers and you may have to add or rename the first "row" column using my change_col node. But you can create matrices entirely within Nodebox using the matrix node.

The matrix node takes 7 inputs:

  • Column Names (Optional). A list of column headers (not including "row"). If no headers are supplied, default values will be used.
  • Row Names (Optional). A list of row headers. If no headers are supplied, default values will be used.
  • Values (Optional). A list of numerical values arranged left to right, top to bottom. If the number of values does not match the size of the matrix, the value list will be trimmed or repeated.
  • Columns. The number of columns (not including the row headers). If a list of column names was provided, this value will be ignored.
  • Rows. The number of rows. If a list of row names was provided, this value will be ignored.
  • Default Cell. The value uses in each cell of the matrix if no other values were provided.
  • Identity. If checked, a square identity matrix will be created with 1s along the main diagonal and 0s in all other cells. If row and/or column headers were provided, they will be used. If the matrix is not defined to be square, it will be sized bases on the number of rows or the number of columns, whichever is less.

A matrix created by the matrix node can be fed directly into any other mat node (mat_add, mat_subtract, etc.). The attached demo contains 11 such nodes:

  • mat_add. Adds two matrices, retaining all headers. Checks to be sure they are the same size.
  • mat_data. Extracts values from a matrix as a single list in left to right, top to bottom order.
  • mat_fill. Fills a matrix from a list of values in left to right, top to bottom order. Uses headers from the supplied matrix. You can set the precision of the values.
  • mat_invert. Inverts a matrix, retraining all headers. You can set the precision of the values. REQUIRES INVERT_MATRIX.PY. For more information see my separate post about this node.
  • mat_multiply. Multiplies two matrices. Checks to be sure the number of columns in matrix A equals the number of rows in matrix B. The resulting matrix will use the row headers from matrix A and the column headers from matrix B.
  • mat_random. Creates a matrix filled with random values. Mat_random takes 7 inputs:
    • Symbol. A string used to define the row and column headers. Rows will be based on the uppercase version, columns on the lowercase version. For example, if the symbol is "A", row headers will be A0, A1, etc. and column headers will be a0, a1, etc.
    • Columns. The number of columns.
    • Rows. The number of rows.
    • Random Start. The minimum value for the range of random numbers.
    • Random End. The maximum value for the range of random numbers.
    • Seed. The seed value for the random numbers.
    • Precision. The precision of the numbers generated.
  • mat_rowSum. Produces a two-column table containing the row headers of a supplied matrix and the sum of the values in each row.
  • mat_subtract. Subtracts two matrices, retaining all headers. Checks to be sure they are the same size.
  • mat_read. Returns the value of one cell in a matrix. If cell is out of range, nothing is returned.
  • mat_trans Transposes a matrix.
  • mat_write. Inserts a value into one cell in a matrix. If cell is out of range, matrix is retained as is.

The attached demo shows seven matrices. The first column of three matrices uses mat_random to create matrices A1 and A2, then uses mat_add to add them. The second column of three matrices does the same for B1 and B2, then uses mat_subtract to subtract them. Finally, mat_multiply is used to multiply matrix A! + A2 by matrix B1 - B2.

I have long wanted matrices in Nodebox and now we have them. I know of some interesting uses and suspect there are many, many more. One use of mat_inverse is shown in a separate post about the Hampton Court Maze.

As always, please reply with unbridled praise, questions, and/or examples of your work using these nodes.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Already uploaded files

  • Matrix_Demo_Screenshot.png 628 KB
  • matrix_demo.zip 520 KB

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

26 Jul, 2026 05:43 PM
24 Jul, 2026 12:33 PM
19 Jul, 2026 10:58 PM
19 Jul, 2026 10:15 PM
18 Jul, 2026 09:52 AM

 

28 Jun, 2026 09:51 AM
24 May, 2026 08:01 AM
19 Apr, 2026 11:02 AM
06 Apr, 2026 08:40 PM
12 Mar, 2026 01:15 AM
10 Mar, 2026 03:15 AM