如何裁剪2D数组的小节? [英] How to crop subsection of 2D array?
本文介绍了如何裁剪2D数组的小节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个2D数组声明为 new byte [w,h]
。我想裁剪给定坐标(x1,y1)
-(x2,y2)
。最快的方法是什么?有什么方法可以切片二维数组?
I have a 2D array declared as new byte[w, h]
. I want to crop out a sub-section of this array given coordinates (x1,y1)
-(x2,y2)
. What's the quickest way to do this? Are there any methods for "slicing" 2D arrays?
推荐答案
您可以使用 Array.Copy :
int w2 = x2 - x1 + 1;
int h2 = y2 - y1 + 1;
byte[,] array2 = new byte[w2, h2];
for (int i = 0; i < w2; i++)
{
Array.Copy(array1, (i+x1)*h + y1, array2, i*h2, h2);
}
对于大型阵列,这可能会更快。但是,两个嵌套循环更具可读性。
This might be faster for large arrays. The two nested loops are however more readable.
这篇关于如何裁剪2D数组的小节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文