麻烦的
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# TODO 构造增广矩阵,假设A,b行数相同 def augmentMatrix(A, b): if ( len (A) ! = len (b)): raise 'The number of rows is different' result = [] for i in range ( len (A)): row = [] for j in range ( len (A[i])): row.append(A[i][j]) for j in range ( len (b[i])): row.append(b[i][j]) result.append(row) return result |
优化后
1
2
3
4
5
6
7
8
|
# TODO 构造增广矩阵,假设A,b行数相同 def augmentMatrix(A, b): return [AA + bb for AA, bb in zip (A,b)] A = [[ 1 , 2 , 3 ],[ 4 , 5 , 6 ],[ 7 , 8 , 9 ]] b = [[ 1 ],[ 2 ],[ 3 ]] print augmentMatrix(A,b) [[ 1 , 2 , 3 , 1 ], [ 4 , 5 , 6 , 2 ], [ 7 , 8 , 9 , 3 ]] |
注:解读一下AA+bb, 在 python中, [1, 2, 3] + [4]这样的表达式会返回[1, 2, 3, 4]
以上这篇在Python中构建增广矩阵的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/chuan403082010/article/details/79407431