Python
#モジュールのインポートimport pandas as p import numpy as np import math from scipy.signal import convolve2d #配列準備land_sea_boundary = np.array([[2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2], [2, 2, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]) #カウンター格納用の配列準備retreat_increment = np.zeros([6, 6]) #mainの処理up = np.array([[0, 1, 0], [0, 0, 0], [0, 0, 0]])down = np.array([[0, 0, 0], [0, 0, 0], [0, 1, 0]])left = np.array([[0, 0, 0], [1, 0, 0], [0, 0, 0]])right = np.array([[0, 0, 0], [0, 0, 1], [0, 0, 0]]) up_right = np.array([[0, 0, 1], [0, 0, 0], [0, 0, 0]])up_left = np.array([[1, 0, 0], [0, 0, 0], [0, 0, 0]])down_right = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 1]])down_left = np.array([[0, 0, 0], [0, 0, 0], [1, 0, 0]]) retreat_increment = retreat_increment + np.where((land_sea_boundary == 1) & (convolve2d(land_sea_boundary, up, mode='same', boundary='fill', fillvalue=0) == 0), 1, 0)retreat_increment = retreat_increment + np.where((land_sea_boundary == 1) & (convolve2d(land_sea_boundary, down, mode='same', boundary='fill', fillvalue=0) == 0), 1, 0)retreat_increment = retreat_increment + np.where((land_sea_boundary == 1) & (convolve2d(land_sea_boundary, left, mode='same', boundary='fill', fillvalue=0) == 0), 1, 0)retreat_increment = retreat_increment + np.where((land_sea_boundary == 1) & (convolve2d(land_sea_boundary, right, mode='same', boundary='fill', fillvalue=0) == 0), 1, 0)retreat_increment = retreat_increment + np.where((land_sea_boundary == 1) & (convolve2d(land_sea_boundary, up_right, mode='same', boundary='fill', fillvalue=0) == 0), 1/math.sqrt(2), 0)retreat_increment = retreat_increment + np.where((land_sea_boundary == 1) & (convolve2d(land_sea_boundary, up_left, mode='same', boundary='fill', fillvalue=0) == 0), 1/math.sqrt(2), 0)retreat_increment = retreat_increment + np.where((land_sea_boundary == 1) & (convolve2d(land_sea_boundary, down_right, mode='same', boundary='fill', fillvalue=0) == 0), 1/math.sqrt(2), 0)retreat_increment = retreat_increment + np.where((land_sea_boundary == 1) & (convolve2d(land_sea_boundary, down_left, mode='same', boundary='fill', fillvalue=0) == 0), 1/math.sqrt(2), 0) #配列の四辺は内側の値をコピーretreat_increment = np.pad(retreat_increment[1:-1,1:-1], 1, 'edge') print(retreat_increment) #[[0. 0. 0. 0. 0. 0. ] #[0. 0. 0. 0. 0. 0. ] #[0. 0. 0.70710678 1.70710678 2.41421356 2.41421356] #[2.41421356 2.41421356 3.41421356 0. 0. 0. ] #[0. 0. 0. 0. 0. 0. ] #[0. 0. 0. 0. 0. 0. ]]

0 コメント