不使用循环切割矩阵的行
我有一个矩阵,我想创建一个新矩阵,该矩阵将是旧矩阵,但没有第一行和第一列。有没有办法在不使用循环的情况下做到这一点?
I have a matrix and i want to create a new matrix which will be the old matrix, but without the first row and first column. is there a way to do this without using loops?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由此看来,您想要一个新的
T[,]
对象。我将其解释为意味着您希望新的
T[,]
对象包含与原始的,除了第一行/列。如果我正确地解释了你的问题,那么不,不是真的。您需要将元素从一个数组复制到另一个数组;这需要枚举。但这并不意味着您不能将此方法的实现抽象为可重用的方法(事实上,这是您应该做的)。
上面的代码并不漂亮,但是一旦就位,您将能够非常巧妙地使用它:
现在,如果我误解了您的问题,并且您并没有执意要创建一个新的
T[,]
对象,您可以通过根本不分配新的T[,]
来提高此方法的效率;您可以采用 Abel 的想法(及其警告)并使用不安全的
代码本质上模拟一个T[,]
,其索引指向原始矩阵的元素。想想看,您甚至可以在不诉诸不安全
代码的情况下实现这一点;您只需要为您想要公开的功能定义一个接口(想到一个 this[int, int] 属性),然后实现该功能(您的返回类型不会在本例中是一个T[,]
,但我想说的是它可能是类似的东西)。From this it sounds to me like you want a new
T[,]
object.I interpret this to mean you want the new
T[,]
object to contain the same values as the original, excepting the first row/column.If I've interpreted your question correctly, then no, not really. You will need to copy elements from one array to another; this requires enumeration. But that doesn't mean you can't abstract the implementation of this method into a reusable method (in fact, this is what you should do).
The above code isn't pretty, but once it's in place you'll be able to use it quite neatly:
Now, if I misinterpreted your question, and you are not dead-set on creating a new
T[,]
object, you can improve the efficiency of this approach by not allocating a newT[,]
at all; you could take Abel's idea (along with its caveats) and useunsafe
code to essentially simulate aT[,]
with indices pointing to the elements of the original matrix. Come to think of it, you could even achieve this without resorting tounsafe
code; you'd simply need to define an interface for the functionality you'd want to expose (athis[int, int]
property comes to mind) and then implement that functionality (your return type wouldn't be aT[,]
in this case, but what I'm getting at is that it could be something like it).简单地说:不。但是,如果您不使用锯齿状数组,而是使用多维数组,并且如果您花一些时间研究 .NET 中数组的内存布局,则可以使用不安全的指针并擦除一部分内存并移动多维数组的起始指针。但这仍然取决于您如何设计数组和矩阵,无论这是否有效。
但是,我强烈建议不要这样做。如果你这样做,你很有可能会搞砸类型并使垃圾收集器感到困惑。
或者,如果您喜欢进行此练习,请使用 C++/CLI 来完成此任务。在 C++ 中,您拥有更多控制权,并且更容易直接操作内存和移动指针。您还可以更好地控制析构函数和终结器,这在这里可能会派上用场。但是,话虽如此,您仍然需要编组。如果您这样做是为了提高性能,我建议回到简单的循环,在大多数情况下它会执行得更快。
Simply put: no. But if you do not use jagged arrays but instead use multi-dim arrays, and if you take some time to study the memory layout of arrays in .NET, you could do it with unsafe pointers and erasing a part of the memory and moving the starting pointer of the multi-dim array. But it'd be still dependent on how you design your arrays and your matrixes whether this works or not.
However, I'd highly advice against it. There's a big chance you screw up the type and confuse the garbage collector if you do so.
Alternatively, if you like to do this exercise, use C++/CLI for this task. In C++, you have more control and it's easier to manipulate memory and move pointers directly. You also have more control over the destructor and finalizers, which may come in handy here. But, that said, then you still need marshaling. If you'd do all this for performance, I'd advice to go back to the simple loops, it'll perform faster in most cases.
也许您应该看看使用对矩阵运算有良好支持的数学库?这里有一个线程提到了一些:
Matrix Library for .NET
Maybe you should have a look at using a maths library with good support for Matrix operations? Here's a thread which mentions a few:
Matrix Library for .NET
使用 缓冲区 类,如果矩阵元素类型是基本类型,则可以进行逐行复制。这应该比逐元素复制更快。这是一个通用的扩展方法,演示了 Buffer 的使用:
Using some methods from the Buffer class, you can do a row-wise copy if the matrix element type is a primitive type. This should be faster than an element-wise copy. Here is a generic extension method which demonstrates the use of Buffer: