There is a weird little command called RDM that's probably not that useful in many contexts, but here it can do some work.
The idea is that a matrix like this:
Code:
[
[ 0. 1. 1. 1. 1. ]
[ 0. 1. 1. 1. 1. ]
[ 0. 1. 1. 1. 1. ]
]
is almost identical to a 4-by-4 anti-identity matrix when read line by line (only the final 0. is missing). And RDM is all we need to reformat this into the desired anti-identity matrix (it creates new elements with value 0. as needed, so the missing element is not a problem; we don't even need to overwrite it afterwards because in this case we want exactly that value). In the end all we have to do is generate this other matrix which looks nicely uniform, so we can just do it with a pair of NDUPN and some \->ARRY and ROW\-> sprinkled in. The rest of the code should be trivial to follow:
Code:
\<<
0. 1. PICK3 NDUPN 1. + \->ARRY
OVER 1. - NDUPN ROW\->
SWAP DUP 2. \->LIST RDM
\>>
Comparing the average timing over 20 runs with input 30. (1.02098_s) with that of the trivial solution using primarily IDN and CON (2.443935_s in my implementation), the RDM approach seems to be quite a bit faster. For reference, this is my implementation of the trivial solution:
Code:
\<<
DUPDUP 2. \->LIST 1. CON
SWAP IDN -
\>>