I'm trying to generate a matrix such that:
I'm trying to modify the example for the identity matrix:
{x=/:x}@til 4
to squeeze in my special function:
shrinkfn: {$[x=y;1;0.5]}
but I'm struggling. What's the best way to do this?
q)m:{x=/:x}@til 4
q)?'[m;1;0.5]
1 0.5 0.5 0.5
0.5 1 0.5 0.5
0.5 0.5 1 0.5
0.5 0.5 0.5 1
Alternative method:
https://code.kx.com/phrases/matrix/#identity-matrix-of-order-x
q)f:{(2#x)#1f,x#.5}
q)f 5
1 0.5 0.5 0.5 0.5
0.5 1 0.5 0.5 0.5
0.5 0.5 1 0.5 0.5
0.5 0.5 0.5 1 0.5
0.5 0.5 0.5 0.5 1
Explanation:
we can use the the following notation to create a matrix:
q)3 3#til 9
0 1 2
3 4 5
6 7 8
when the list runs out of elements it repeats:
q)3 2#til 4
0 1
2 3
0 1
with 5 by 5 matrix the the next diagonal is always 6 places, thus the list is of length 6:
q)5 5#1 .5 .5 .5 .5 .5
1 0.5 0.5 0.5 0.5
0.5 1 0.5 0.5 0.5
0.5 0.5 1 0.5 0.5
0.5 0.5 0.5 1 0.5
0.5 0.5 0.5 0.5 1
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments