我有一个带有值的numpy矩阵。它们不会完全相同,但如果我这样展示它就会更容易:
input = np.array([
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]
])
现在我有另一个相同大小的矩阵。数字有一个“接缝”,每列一个数字(不多,不少)。它们在seam
矩阵中的位置是我想将它们插入input
矩阵的位置。输入矩阵中的所有值都移到右侧。
例如,如果您应用此“接缝”
seam = np.array([
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]
])
到input
矩阵,我想要这个输出:
output = np.array([
[1, 2, 1, 1, 1],
[1, 1, 3, 1, 1],
[1, 1, 1, 4, 1]
])
输入和接缝矩阵将始终具有完全相同的尺寸。输出将具有相同的高度和宽度+ 1的输入。
是否有一种有效的方法来执行此插入?
答案 0 :(得分:2)
以下是使用遮罩重新排列输入值的直接方式:
>>> m, n = seam.shape
>>> output = np.empty((m, n+1), input.dtype)
>>> mask = np.ones((m, n+1), dtype=bool)
>>> nz = np.where(seam)
>>> mask[nz] = False
>>> output[mask]=input.ravel()
>>> output[nz]=seam[nz]
>>> output
array([[1, 2, 1, 1, 1],
[1, 1, 3, 1, 1],
[1, 1, 1, 4, 1]])
答案 1 :(得分:2)
创建零初始化数组并根据其索引分配项目:
public void GenerateSignature()
{
//curve order
BigInteger n = ec.N;
Ramdom rand = new Random();
//private key
BigInteger d = ((ECPrivateKeyParameters)key).D;
//loop for 1 million signatures
for (int i = 1; i <= 1000000; i++)
{
//random k and e
BigInteger e = new BigInteger(112, rand).Mod(n); //new biginteger by giving bitlength and random
BigInteger k = new BigInteger(112, rand).Mod(n);
//calculate r
BigInteger r = key.Parameters.G.Multiply(k).X.ToBigInteger().Mod(n);
//calculate s
BigInteger s = k.ModInverse(n).Multiply(e.Add(d.Multiply(r))).Mod(n);
//save generated signatures to database
new DBCon().ExecuteNonQuery("Insert into signatures values ('" + e.ToString() + "', '" + r.ToString() + "', '" + s.ToString() + "')");
}
}
答案 2 :(得分:1)
这是我的工作:
在steam中获取非空值的索引:
private List<LatLng> DecodePolyline(string encodedPoints)
{
if (string.IsNullOrWhiteSpace(encodedPoints))
{
return null;
}
int index = 0;
var polylineChars = encodedPoints.ToCharArray();
var poly = new List<LatLng>();
int currentLat = 0;
int currentLng = 0;
int next5Bits;
while (index < polylineChars.Length)
{
// calculate next latitude
int sum = 0;
int shifter = 0;
do
{
next5Bits = polylineChars[index++] - 63;
sum |= (next5Bits & 31) << shifter;
shifter += 5;
}
while (next5Bits >= 32 && index < polylineChars.Length);
if (index >= polylineChars.Length)
{
break;
}
currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
// calculate next longitude
sum = 0;
shifter = 0;
do
{
next5Bits = polylineChars[index++] - 63;
sum |= (next5Bits & 31) << shifter;
shifter += 5;
}
while (next5Bits >= 32 && index < polylineChars.Length);
if (index >= polylineChars.Length && next5Bits >= 32)
{
break;
}
currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
var mLatLng = new LatLng(Convert.ToDouble(currentLat) / 100000.0, Convert.ToDouble(currentLng) / 100000.0);
poly.Add(mLatLng);
}
return poly;
}
获取相应的值
indices = np.nonzero(seam.ravel())
并在给定位置插入值:
values = seam[np.nonzero(seam)]