如何将向量分成基向量?

问题描述

我有依据:

// if scroll view overflow on other widget then use ClipRRect With Expanded.

    ClipRRect(
    child: Expanded(
    child: ListView(
    clipBehavior: Clip.none,//add this line inside AnyScrollView or ListView
      children:[
         Container(
             decoration: Boxdecoration(BoxShadow: [
                BoxShadow(
                      offset: Offset(2,2),blurRadius: 12,color: Color.fromrGBO(0,0.16),)
               ]),child: Widgets(),),],

如何访问向量的叠加?让我们说:

>>> 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.]]

所以我想要输出

vec = [0,1,0]

解决方法

我们可以用vec的元素个数构造一条对角线,然后过滤行,所以:

>>> vec = np.array([0,1,0])
>>> np.identity(6)[vec.astype(bool)]
array([[0.,1.,0.,0.],[0.,0.]])

然而,如果 vec 中的项目数量很大,这不是很有效,因为构建单位矩阵需要在 vec 的长度上进行二次时间。

我们也可以像@AlexAlex 所说的那样使用 .diag(…)

>>> vec = np.array([0,0])
>>> np.diag(vec)[vec.astype(bool)]
array([[0,0],[0,0]])

这里我们可以指定不同的值,例如:

>>> vec = np.array([0,2,3,0]])