How do I generate a diagonal matrix in KDB?

cjm2671

I'm trying to generate a matrix such that:

  • Diagonal elements are 1
  • All other elements are 0.5

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?

C.Ross
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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I extract the main diagonal of a sparse matrix?

How can I convert a column vector to the diagonal of a matrix?

How do I make a mask of diagonal matrix, but starting from the 2nd column?

In a matrix having a row of zeros, how do I replace the corresponding diagonal entry of the matrix with a one?

How can I create a lower diagonal matrix in Sympy?

How do I generate an adjacency matrix of a graph from a dictionary in python?

Python: How do I create a diagonal matrix with block matrices as diagonals

How do I use a markov chain matrix to generate music?

How do I parse datetime in KDB Q?

Python: How do I generate a random matrix of 1.0 and 0.0, but with floats?

How do I join symbols in KDB?

How do I round numbers in KDB?

How do I drop rows by symbol in KDB?

How do I calculate eigenvectors with KDB?

How do I turn a dictionary into a table in KDB?

How do I convert a list into a matrix in KDB?

How do I make a dictionary of zeroes in KDB?

How do I filter a list in KDB?

How do I replace items in a list in KDB?

How do I unassign variables in KDB?

How do I generate a list of times by minute in KDB?

How do I fix diagonal movement?

How do I stack matrices in KDB?

How do I do argmax in KDB?

How do I evaluate a function into a matrix in KDB?

How do I construct a diagonal vector that has entries exceed the maximum number of entries allowed in a matrix in R?

How can I create a diagonal matrix in Eigen

How can I print a diagonal matrix in Eigen

How to generate a matrix of random values with zeros on the diagonal?