开发者

Cut rows of a matrix without using loops

I have a matr开发者_如何学JAVAix 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?


i want to create a new matrix

From this it sounds to me like you want a new T[,] object.

which will be the old matrix, but without the first row and first column

I interpret this to mean you want the new T[,] object to contain the same values as the original, excepting the first row/column.

is there a way to do this without using loops?

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).

public static T[,] SubMatrix(this T[,] matrix, int xstart, int ystart)
{
    int width = matrix.GetLength(0);
    int height = matrix.GetLength(1);

    if (xstart < 0 || xstart >= width)
    {
        throw new ArgumentOutOfRangeException("xstart");
    }
    else if (ystart < 0 || ystart >= height)
    {
        throw new ArgumentOutOfRangeException("ystart");
    }

    T[,] submatrix = new T[width - xstart, height - ystart];

    for (int i = xstart; i < width; ++i)
    {
        for (int j = ystart; j < height; ++j)
        {
            submatrix[i - xstart, j - ystart] = matrix[i, j];
        }
    }

    return submatrix;
}

The above code isn't pretty, but once it's in place you'll be able to use it quite neatly:

T[,] withoutFirstRowAndColumn = originalMatrix.SubMatrix(1, 1);

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 new T[,] at all; you could take Abel's idea (along with its caveats) and use unsafe code to essentially simulate a T[,] with indices pointing to the elements of the original matrix. Come to think of it, you could even achieve this without resorting to unsafe code; you'd simply need to define an interface for the functionality you'd want to expose (a this[int, int] property comes to mind) and then implement that functionality (your return type wouldn't be a T[,] in this case, but what I'm getting at is that it could be something like it).


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.


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


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:

static PrimitiveType[,] SubMatrix<PrimitiveType>(
    this PrimitiveType[,] matrix, int fromRow, int fromCol) where PrimitiveType: struct
{
    var (srcRowCount, srcColCount) = ( matrix.GetLength(0), matrix.GetLength(1) );
    if (fromRow < 0 || fromRow > srcRowCount)
    {
        throw new IndexOutOfRangeException(nameof(fromRow));
    }
    if (fromCol < 0 || fromCol > srcColCount)
    {
        throw new IndexOutOfRangeException(nameof(fromCol));
    }            
    var (dstRowCount, dstColCount) = ( srcRowCount - fromRow, srcColCount - fromCol );
    var subMatrix = new PrimitiveType[dstRowCount, dstColCount];
    var elementSize = Buffer.ByteLength(matrix) / matrix.Length;
    for (var row = 0; row < dstRowCount; ++row)
    {
        var srcOffset = (srcColCount * (row + fromRow) + fromCol) * elementSize;
        var dstOffset = dstColCount * row * elementSize;
        Buffer.BlockCopy(matrix, srcOffset, subMatrix, dstOffset, dstColCount * elementSize);
    }
    return subMatrix;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜