# Matrices and Lists

Whenever you need to store a lot of data, you will probably find that one of these two structures is best suited to your needs. Matrices (in this form) have only been available since the fx-9700 and lists are only available on the 9x50s.

## Matrices

Matrices are grids of values with fixed dimensions. You can specify their contents manually from matrix mode, which saves a lot of hassle when dealing with large matrices. If you are writing a program in Calculator Text Format (CTF) and you want the matrix set up directly in this way, you should specify it with code of the form:
```  @@ Matrix C (4 x 5)

#       1      2        3      4     5
#======================================
[  42          0        0     -1     0]
[ 123.456      0  3.45E-6      0     0]
[   5.03   +9876        0      0     0]
[ -27.3    1%2%3        0      0     0]
```
The example above is specific to writing programs on computers. If you never publish your programs on the web or write programs on the computer for any other reason you can ignore it.

If you were writing the code to generate this matrix into a program you would need to use this line:

```  [[42,0,0,-1,0],[123.456,0,3.45\exp-6,0,0],[5.03,+9876,0,0,0],
[-27.3,1%2%3,0,0,0]] -> Mat C
```

To directly change one cell in a matrix you need to use a line such as this, which stores 1.23 in the second row, third column:

```  1.23 -> Mat A[2,3]
```

### Specifying matrix dimensions from programs

It isn't usually possible to set the dimensions of a matrix on the calculator without specifying the contents of each cell. That is to say, if you want to create a blank 20-by-20 matrix you need to specify all 400 zeros explicitly with a line starting [[0,0,0,0,0,0,...

The following code for 9x50 calcs uses lists to create such an array:

```  "HEIGHT"?->H
"WIDTH"?->W
Seq(0,X,1,H,1->List 1      ; Creates an empty list of length H
List->Mat(List 1)->Mat A   ; Converts list to single-column matrix in Mat A
Mat A                      ; Stores single-column matrix in Ans matrix
For 1->X To W-1
Augment(Mat Ans,Mat A    ; Adds another W-1 columns to Ans matrix
Next
Mat Ans->Mat A             ; Stores completed matrix in Mat A.
#
# Optional lines to free memory:
#
{0}->List 1                ; Deletes all but one entry from list 1.
[[0                        ; Deletes copy of Mat A from Ans memory.
```

## Lists

Lists are only available on 9x50 calcs.

### Built-in commands

#### Editing members of a list

To directly change the 5th member of List 1 to 77:
```  77->List 1[5]
```

### Other manipulations

A major weakness of the cfx-9850 and cfx-9950 is that there are no commands for:

• Appending an item to the end of a list
• Concatenating two lists
• Inserting an item into a list
• Removing an item from a list

Fortunately, there are ways to get around all of these problems, to greatly increase the power of lists as programming tools.

#### Appending an item to a list

To append 77 to List 1 (matrix method):
```  Trn Augment(List->Mat(List 1),[[77->Mat A
Mat->List(Mat A,1
```

This has the disadvantage that it requires a spare matrix, taking up more memory than necessary. Because of this, you may prefer instead to use the following method:

To append 77 to List 1 (sequence method):

```  1+Dim List 1
Seq(77Int X%Ans + (X<>Ans)List 1[X - (X=Ans, X, 1, Ans, 1->List 1
```

This has a speed disadvantage when compared to the matrix method however.

#### Concatenating two lists

Concatenating two lists is a more complicated version of the append function. Similarly, there are two methods, a matrix method and a sequence method.

To concatenate List 1 and List 2 and store the result in List 3 (matrix method):

```  Trn Augment(Trn List->Mat(List 1),Trn List->Mat(List 2)->Mat A
Mat->List(Mat A,1->List 3
```

To concatenate List 1 and List 2 and store the result in List 3 (sequence method):

```  Dim List 1
Seq(List 2[X - Ans + (X<=Ans)(1 - X + Ans](X>Ans) + (X<=Ans)List 1
[X - (X>Ans)(X - 1, X, 1, Ans + Dim List 2, 1->List 3
```

#### Inserting an item into a list

To insert 77 into the 5th place in List 1:
```  Seq(77(X=5)+(X<>5)List 1[X - (X > 5, X, 1, Dim List 1 + 1, 1->List 1
```

#### Removing an item from a list

To delete the 5th item in List 1:
```  Seq(List 1[X+(X>=5, X, 1, Dim List 1 - 1, 1->List 1
```
Note that you can't delete the first item in a list if it is the only item in the list. I must admit that at the moment I can't remember/think how to do that, as I think ClrList clears all lists and { } -> List 1 gives a syntax error. It could be the one thing you can't do with lists... If it is, you might have to keep a dummy value in the first item.

To delete the last item in a list:

```  Seq(List 1[X, X, 1, Dim List 1 - 1, 1->List 1
```