numpy-创建计数上部矩阵
这就是问题:您需要一种算法,要求用户提供方阵的行数,然后用计数整数填充矩阵的上三角形。您的矩阵应该是 2D Numpy 数组。
注意:
由于矩阵是正方形大小,行数和列数相同,因此您可以首先使用算法应使用 for 循环的适当 numpy 方法将矩阵初始化为所有零条目。
在循环内部,一条消息应显示当前行和列索引以及要存储在该位置的值。
循环完成后,应显示整个矩阵 您的算法将提示用户输入矩阵大小并适应输入的大小
提示:如果您难以完成此任务,请将其分解为更简单的类似任务。
这些可能是:
- 用值 1 填充整个矩阵(在循环中)
- 打印一个方阵,其中整个矩阵用递增的整数填充
完成这些任务后,您可能会对如何解决该任务有更好的想法在这个问题中。
这是我的代码:
#input size of matrix from the user
M = int(input('Enter Matrix size: '))
N = M
#The dimension of the matrix in nXm
dimension = (M, N)
#form a matrix of size nXm
matrix = np.zeros(dimension, dtype=int)
#use the num for filling the values in the matrix
num = 1
#for every row
for i in range (M):
#the elements before the diagonal are 0
for j in range (i+1):
#print ("The value at row " + str(i) + " and column " + str(j) + " is 0", end = '\n')
#the elements after the diagonal are num
for j in range (i,N):
#print ("The value at row " + str(i) + " and column " + str(j) + " is " + str(num), end = '\n')
matrix[i][j] = num
#increase num by 1
num = num + 1
#print the upper triangular matrix
print (matrix)
它打印的矩阵是: [1, 1, 1 0, 3, 3 0, 0, 6]
虽然我想打印的矩阵是: [1,2,3 0, 4, 5 0, 0, 6]
This is the question: you require an algorithm that asks the user for the the number of rows of a square matrix and then fills the upper triangle of the matrix with counting integers. Your matrix should be a 2D Numpy array.
Notes:
since the matrix is of square size, the number of rows and columns are identical, you may start with initializing the matrix to all zero entries using the appropriate numpy method your algorithm should be using for loops.
Inside the loop, a message should display the current row and column index and the value to be stored at that position.
After the loops have completed, the entire matrix should be displayed
your algorithm will prompt the user for the matrix size and adapt to the size entered
Hint: if you struggle with this task, break it down into easier similar tasks.
These could be:
- fill the entire matrix with values of 1 (in the loop)
- print a square matrix where the entire matrix is filled with increasing integer numbers
Once you have accomplished these tasks, you might have a better idea on how to solve the task in this problem.
This is my code:
#input size of matrix from the user
M = int(input('Enter Matrix size: '))
N = M
#The dimension of the matrix in nXm
dimension = (M, N)
#form a matrix of size nXm
matrix = np.zeros(dimension, dtype=int)
#use the num for filling the values in the matrix
num = 1
#for every row
for i in range (M):
#the elements before the diagonal are 0
for j in range (i+1):
#print ("The value at row " + str(i) + " and column " + str(j) + " is 0", end = '\n')
#the elements after the diagonal are num
for j in range (i,N):
#print ("The value at row " + str(i) + " and column " + str(j) + " is " + str(num), end = '\n')
matrix[i][j] = num
#increase num by 1
num = num + 1
#print the upper triangular matrix
print (matrix)
The matrix it prints is :
[1, 1, 1
0, 3, 3
0, 0, 6]
While the matrix I would like to print is:
[1, 2, 3
0, 4, 5
0, 0, 6]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您想生成一个上三角方形矩阵。
n = 4
的输出I think you want to generate an upper triangle square matrix.
Output for
n=4