I have the basis:
>>> m = 6
>>> basis = np.zeros((m, m))
>>> np.fill_diagonal(basis, 1)
array([[1. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 1.]]
How can I access the superposition of a vector? Let's say from:
vec = [0, 1, 1, 0, 1, 0]
So I want the output:
array([[0. 1. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[0. 0. 0. 0. 1. 0.]]
We can construct a diagonal with the number of elements of
vec
, and then filter the rows, so:This is however not very efficient if the number of items in the
vec
is large, since constructing an identity matrix takes quadratic time in the length ofvec
.We can also work with
.diag(…)
as @AlexAlex says:Here we can specify different values, for example: