numpy-创建计数上部矩阵

发布于 2025-01-17 22:41:05 字数 1315 浏览 0 评论 0原文

这就是问题:您需要一种算法,要求用户提供方阵的行数,然后用计数整数填充矩阵的上三角形。您的矩阵应该是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜血缘 2025-01-24 22:41:05

我认为您想生成一个上三角方形矩阵。

import numpy as np


#input size of matrix from the user
n = int(input('Enter Matrix size: '))


#form a matrix of size nXn
matrix = np.zeros((n,n), dtype=int)

#use the num for filling the values in the matrix
num = 1
for i in range(n):
    #the elements before the diagonal are 0 
    for j in range(i):
      print (f"The value at row  {i} and column {j} is 0")

    for j in range (i, n):
      print (f"The value at row  {i} and column {j} is {num}")
      matrix[i][j] = num
      num = num + 1

print (f"matrix=\n{matrix}")

n = 4的输出

Enter Matrix size: 4
The value at row  0 and column 0 is 1
The value at row  0 and column 1 is 2
The value at row  0 and column 2 is 3
The value at row  0 and column 3 is 4
The value at row  1 and column 0 is 0
The value at row  1 and column 1 is 5
The value at row  1 and column 2 is 6
The value at row  1 and column 3 is 7
The value at row  2 and column 0 is 0
The value at row  2 and column 1 is 0
The value at row  2 and column 2 is 8
The value at row  2 and column 3 is 9
The value at row  3 and column 0 is 0
The value at row  3 and column 1 is 0
The value at row  3 and column 2 is 0
The value at row  3 and column 3 is 10
matrix=
[[ 1  2  3  4]
 [ 0  5  6  7]
 [ 0  0  8  9]
 [ 0  0  0 10]]

I think you want to generate an upper triangle square matrix.

import numpy as np


#input size of matrix from the user
n = int(input('Enter Matrix size: '))


#form a matrix of size nXn
matrix = np.zeros((n,n), dtype=int)

#use the num for filling the values in the matrix
num = 1
for i in range(n):
    #the elements before the diagonal are 0 
    for j in range(i):
      print (f"The value at row  {i} and column {j} is 0")

    for j in range (i, n):
      print (f"The value at row  {i} and column {j} is {num}")
      matrix[i][j] = num
      num = num + 1

print (f"matrix=\n{matrix}")

Output for n=4

Enter Matrix size: 4
The value at row  0 and column 0 is 1
The value at row  0 and column 1 is 2
The value at row  0 and column 2 is 3
The value at row  0 and column 3 is 4
The value at row  1 and column 0 is 0
The value at row  1 and column 1 is 5
The value at row  1 and column 2 is 6
The value at row  1 and column 3 is 7
The value at row  2 and column 0 is 0
The value at row  2 and column 1 is 0
The value at row  2 and column 2 is 8
The value at row  2 and column 3 is 9
The value at row  3 and column 0 is 0
The value at row  3 and column 1 is 0
The value at row  3 and column 2 is 0
The value at row  3 and column 3 is 10
matrix=
[[ 1  2  3  4]
 [ 0  5  6  7]
 [ 0  0  8  9]
 [ 0  0  0 10]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文