zoukankan      html  css  js  c++  java
  • Python中怎样统计两个向量对应位置的非0元素个数??

    首先看看矩阵中.A操作的结果

     1 >>> a=mat([[1,2,3],[2,3,0]]);
     2 >>> a
     3 matrix([[1, 2, 3],
     4         [2, 3, 0]])
     5 >>> a.A
     6 array([[1, 2, 3],
     7        [2, 3, 0]])
     8 >>> shape(a)
     9 (2, 3)
    10 >>> shape(a.A)
    11 (2, 3)
    12 >>> type(a)
    13 <class 'numpy.matrixlib.defmatrix.matrix'>
    14 >>> type(a.A)
    15 <class 'numpy.ndarray'>

     nonzero()将布尔数组转换成一组整数数组,然后使用整数数组进行下标运算;

    1 >>> a=mat(([False,  True,  True],[True,  True,False]))
    2 >>> a
    3 matrix([[False,  True,  True],
    4         [ True,  True, False]], dtype=bool)
    5 >>> nonzero(a)
    6 (matrix([[0, 0, 1, 1]], dtype=int32), matrix([[1, 2, 0, 1]], dtype=int32))

    >>> nonzero(a)[0]
    matrix([[0, 0, 1, 1]], dtype=int32)

  • 相关阅读:
    CSS部分
    css基础
    css初探
    html表单
    mysql视图 触发器 事物 函数 存储过程
    pymysql
    mysql数据备份与操作
    html基础
    mysql 索引
    tuple,list,dict,set用法
  • 原文地址:https://www.cnblogs.com/yuzhuwei/p/4127398.html
Copyright © 2011-2022 走看看