在Python中,你可以使用numpy库来创建和操作矩阵。以下是一个示例代码,它定义了一个5行4列的矩阵,并将四周元素设置为1,其余元素设置为0:

python

import numpy as np

# 定义一个5x4的全零矩阵

matrix = np.zeros((5, 4))

# 设置四周元素为1

for i in range(matrix.shape[0]):

for j in range(matrix.shape[1]):

if (i == 0 or i == matrix.shape[0] - 1) or (j == 0 or j == matrix.shape[1] - 1):

matrix[i][j] = 1

print("Matrix:")

print(matrix)

运行这段代码后,你会得到如下输出:

code

Matrix:

[[1. 1. 1. 1.]

[1. 0. 0. 1.]

[1. 0. 0. 1.]

[1. 0. 0. 1.]

[1. 1. 1. 1.]]

这表示你已经成功地创建了一个5行4列的矩阵,其中四周元素值为1,其它元素值为0。