使用指数,循环和格式:
import numpy as np
def printMatrix(a):
print "Matrix["+("%d" %a.shape[0])+"]["+("%d" %a.shape[1])+"]"
rows = a.shape[0]
cols = a.shape[1]
for i in range(0,rows):
for j in range(0,cols):
print "%6.f" %a[i,j],
print
print
def printMatrixE(a):
print "Matrix["+("%d" %a.shape[0])+"]["+("%d" %a.shape[1])+"]"
rows = a.shape[0]
cols = a.shape[1]
for i in range(0,rows):
for j in range(0,cols):
print("%6.3f" %a[i,j]),
print
print
inf = float('inf')
A = np.array([[0,1.,4.,inf,3],
[1,0,2,inf,4],
[4,2,0,1,5],
[inf,inf,1,0,3],
[3,4,5,3,0]])
printMatrix(A)
printMatrixE(A)
其产生的输出:
Matrix[5][5]
0 1 4 inf 3
1 0 2 inf 4
4 2 0 1 5
inf inf 1 0 3
3 4 5 3 0
Matrix[5][5]
0.000 1.000 4.000 inf 3.000
1.000 0.000 2.000 inf 4.000
4.000 2.000 0.000 1.000 5.000
inf inf 1.000 0.000 3.000
3.000 4.000 5.000 3.000 0.000
你是故意使用'j'作为两个嵌套循环递增变量? – Kudzu
控制数组中每个项目的价值的数学公式是什么? – inspectorG4dget
谢谢你接受我的错误。我的意思是ti使用“我” – user2548833