# HP Forums

Full Version: Create a matrix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Specifically, I would like to create a matrix, using a 3 element specified pattern, (tridiagonal), with a specified number of rows and columns. The center value of the pattern should be on the main diagonal of the matrix:

Some kind of variation on this (non-working) theme:
makemat([-1,2,-1], 5,5); // Uses the pattern -1,2,-1 in a 5x5 matrix, with 2 on the main diagonal:

[[2,-1,0,0,0],
[-1,2,-1,0,0],
[0,-1,2,-1,0],
[0,0,-1,2,-1],
[0,0,0,-1,2]]

I (think) this can be done using the matlab eye() command, for example. Any ideas how to create a custom matrix in similar fashion, on the prime?

-Dale-
Try:
MAKEMAT(IFTE(I=J,2,IFTE(J=I-1 OR J=I+1,-1,0)),5,5)
Or:
MAKEMAT(IFTE(I=J,2,IFTE(ABS(I-J)=1,-1,0)),5,5)

Something interesting happens when I copy from your post, into the [CAS] (emulator):
MAKEMAT(IFTE(I=J,2,IFTE(J=I-1 OR J=I+1,-1,0)),5,5) :

CAS changed changed all of the IFTE()'s to when()'s:
MAKEMAT(when(I=J,2,when(J=I-1 OR J=I+1,-1,0)),5,5)

Then I recalled that result to the command line, it changed all of the when()'s to:
MAKEMAT(((I = J)? 2 : (((J = (I-1)) OR (J = (I+1)))? -1 : 0)),5,5)

I've never seen that last format before, on the prime. Maybe an XCAS shorthand?
The programming language C has this same ternary operator

Cond ? a : b

If Cond is true returns a, otherwise returns b.

https://en.wikipedia.org/wiki/%3F:
Reference URL's
• HP Forums: https://www.hpmuseum.org/forum/index.php
• :