【发布时间】:2017-05-03 07:46:21
【问题描述】:
我生成了一个 4 x 4 数组。然后我生成了一个长度为 8 的 1 和 0 的一维数组,例如 [0 1 0 1 0 1 1 0]。然后我生成了一个种子,用于创建随机索引样本。生成的索引数等于消息中的元素数。现在我希望在生成的索引中添加消息的元素,这些元素表示 4 x 4 数组中索引的位置。在 4 x 4 矩阵的索引中添加消息的元素后。我希望使用添加的元素显示新的 4 x 4 数组矩阵。
import numpy as np
import random
from PIL import Image
import matplotlib.pyplot as plt
#array
cover = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
max_len = (4 * 4) #number of elements in cover since we have a 4 by 4 array
#Generating Message
msg = np.random.randint(2, size=8)
len_msg = len(msg)
#Generating a seed which will be used to locate where the msg will be added in the matrix
seed = 4
print "The chosen seed is %d" % (seed)
random.seed(seed)
#Generate Indices to be used to add in matrix
indices = random.sample(range(len_msg, max_len),len_msg)
【问题讨论】:
-
有什么问题?
-
正确提及您的问题。无法理解
-
Indices 是 4 x 4 数组的索引,我想在其中添加消息的元素。将消息的元素添加到这些索引后,我想将这些添加的数据元素添加到原始 4 x 4 数组中,形成一个新数组。
-
老实说,我再清楚不过了..
-
因此,您想要展平第一个 2D 数组,以便可以使用您的全部索引,并且您想用您生成的替换该展平数组中的 8 个值在重塑回二维数组之前,随机索引处的值?我理解正确吗?
标签: python arrays numpy random