python矩阵行数_Python:将矩阵中的每一行与其他所有行进行...

对于此矩阵,K =

[[-1. 1. 0.]

[ 0. 0. 0.]

[ 0. -1. 1.]

[ 0. 0. 0.]

[ 0. -1. 1.]

[ 0. 0. 0.]]

任务是将非零唯一行的索引存储在数组中(此处答案为{0,2}),以便

K([0,2],:)

可用于线性代数运算.

我的尝试是:

myList = []

for i in range(len(K)): #generate pairs

for j in range(i+1,len(K)): #travel down each other rows

if np.array_equal(K[i],K[j]) and np.any(K[i] != 0, axis=1) and np.any(K[j] != 0, axis=1):

myList.append(K[i])

print ('indices of similar-non-zeros rows are

',(i, j)),

elif not np.array_equal(K[i],K[j]) and np.any(K[i] != 0,axis=1) and np.any(K[j] != 0, axis=1):

myList.append(K[i])

print ('indices of non-similar-non-zeros rows are

',(i, j)),

else:

continue

new_K = np.asmatrix(np.asarray(myList))

new_new_K = np.unique(new_K,axis=0)

print('Now K is

',new_new_K)

答案是:

new_new_K = [[-1. 1. 0.]

[ 0. -1. 1.]]

问题1:如何以pythonic方式进行操作.以上是具有矩阵存储限制的替代解决方案,但将索引存储在数组中更为可取.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值