简体   繁体   中英

How to transform an area within a matrix?

I'm writing a code with the aim of simulating the structural change of a surface as it evaporates. I currently have generated random points in a 100x100 matrix of ones in python and then grown zeros out from these points to symbolise the two different surface structures. I now need to add a step within the function that has an arbitrary probability of the area of zeros transforming back to ones when it grows past a certain area. I have tried the following but this just adds random ones within the area of zeros rather than changing the whole area.

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

This section of code is the initial radial growth from certain coordinates within the matrix ( i and j ) and it is the last 4 lines where I am trying to revert the areas of zeros back to ones

I am not sure if this even remotely helps you but let me know-

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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM