如何变换矩阵中的区域?

2024-05-18 18:22:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个代码,目的是模拟表面蒸发时的结构变化。我目前已经在python中生成了一个100x100的1矩阵中的随机点,然后从这些点中增长出0来表示两个不同的表面结构。我现在需要在函数中添加一个步骤,当它增长超过某个区域时,具有任意概率的0区域转换回1。我尝试了以下方法,但这只是在零区域内添加随机数,而不是更改整个区域

for t in range(10):
    for i in range(A):
        for j in range(A):
            if S[i,j] == 0:
                for m in range(-(v*t), (v*t)+1):
                    for n in range(-(v*t), (v*t)+1):
                        if i+m>0 and i+m<A   and j+n>0 and j+n<A:
                            if S[i+m, j+n]==1 and np.sqrt(m**2 + n**2)<=(v*t):
                                S_NEW[i+m, j+n] = 0
                            if np.sqrt(m**2 + n**2) >= 6:
                                R_new = random.random()
                                if R_new > 0.6: #arbitrary prob
                                    S_NEW[i+m, j+n] = 1

这段代码是从矩阵(ij)中的某些坐标开始的初始径向增长,这是我试图将零区域还原为1的最后4行


1条回答
网友
1楼 · 发布于 2024-05-18 18:22:51

我不确定这是否对你有帮助,但请告诉我-

import numpy as np
import matplotlib.pyplot as plt

mat = np.zeros((100, 100))
new_mat = mat.copy()

# you can assign to an area of points i.e. a collection of indices like this
new_mat[40:60, 40:60] = 1

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax1.imshow(mat)
ax2 = fig.add_subplot(122)
ax2.imshow(new_mat)
plt.show()

enter image description here

相关问题 更多 >

    热门问题