有效地将矩阵值复制到新矩阵
是否有一种有效的内置方法可以将值从一个矩阵(例如,double[,]
)复制到另一个矩阵?
换句话说,我正在寻找以下函数的替代品:
public static double[,]CloneMatrix(double[,] aMatrix)
{
var newMatrix = new double[aMatrix.GetLength(0),aMatrix.GetLength(1)];
for (int i = 0; i < aMatrix.GetLength(0); i++)
{
for (int j = 0; j < aMatrix.GetLength(1); j++)
{
newMatrix[i, j] = aMatrix[i, j];
}
}
return newMatrix;
}
Is there an efficient built in method that copies the value from one matrix (e.g., double[,]
) to another?
In order words, I'm looking for a replacement of the below function:
public static double[,]CloneMatrix(double[,] aMatrix)
{
var newMatrix = new double[aMatrix.GetLength(0),aMatrix.GetLength(1)];
for (int i = 0; i < aMatrix.GetLength(0); i++)
{
for (int j = 0; j < aMatrix.GetLength(1); j++)
{
newMatrix[i, j] = aMatrix[i, j];
}
}
return newMatrix;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用克隆方法:
Use the Clone method:
多维数组本身比向量(我们称之为“单维”数组,实际上与向量有点不同)慢,因为它们的实现方式(具有不同的边界检查和访问规则)。因此,顺便说一句,如果考虑效率的话,在某些情况下您可能需要考虑使用
double[][]
。Multidimensional arrays are themselves slower than vectors (what we call "single-dimensional" arrays, which are in fact a little different from vectors) because of how they are implemented (with different bounds checking and access rules). So, as a side note, you might want to consider using a
double[][]
in some cases, if efficiency is a concern.