MathStudio  Blog  Share  Manual  Forums  Support 
Post Format

To detect or not to detect matrices

MathStudio automatically interprets a list of lists as matrix when each list is of the same size.  For example, an input of a={1,2,4,9} is interpreted as a list with four objects, where for example a(3) = 4.

A matrix can be formed by creating a list of lists, as in A = {{1,2,4,9},{1,1,2,5},{3,4,5,6}}.  MathStudio automatically converts this to a matrix type.

The reason this matters is because of operations like multiplication.  When you try to multiply two lists together, it will multiply them component wise, so a*a will return the list \{1,4,16,81\}, but A*A will return an error since A is a 3 by 4 matrix.

Also of interest is extracting sub-matrices and sub-lists.  For example, A(1) will return a list that consists of the first row of A.  To obtain a matrix type, you can use [A(1)].  Alternatively, you can use A(1,all) to get the first row as a list.

Sometimes, though, you want to just work with lists and not worry about MathStudio auto-detecting matrices.  This is especially worrisome when writing scripts, when the user may input a list of lists of the same size and yet the code is written to assume list formats.  In these cases the command to be used is

Command(MatrixDetection=0)

When this line appears at the top of a cell, it tells MathStudio NOT to auto detect matrices.  This way you can be certain that sub-lists will indeed be list types and not matrix types.  See the attached screenshot for an example.

Leave a Reply