连接一二维数组 [英] Concatenate a 2D array

查看:131
本文介绍了连接一二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组MAT1和放大器; MAT2。 我想有new_mat = [MA1,MAT2]。 我写了一个函数,它的工作原理。我不知道是否有一个有效的功能,非常大的矩阵或如何我能做到这一点与Array.CopyTo方法。

 公共静态双[,] Concatenate_matrix_byCol(双[,] MAT1,双[,] MAT2)
{
    INT COL1 = Mat1.GetLength(1);
    INT COL2 = Mat2.GetLength(1);
    INT ROW1 = Mat1.GetLength(0);
    INT ROW2 = Mat2.GetLength(0);
    INT I,J,Y;
    双[,] newMat =新的双[ROW1中,col1 + COL2]。

    对于(i = 0; I< ROW1,我++)
    {
        为(J = 0; J< COL1; J ++)
        {
            newMat [I,J] = MAT1 [I,J]。
        }
    }
    对于(i = 0; I< ROW1,我++)
    {
        对于(Y = 0; Y< COL2; Y ++)
        {
            newMat [I,Y + COL1] = MAT2 [I,Y];
        }
    }
    返回newMat;
}
 

解决方案

在移动阵列,你应该考虑的 Array.CopyTo 的。

你也可以创建一个接受2矩阵,并提供了抽象化,使它们看起来像1矩阵的水平一类,但只是让他们下独立。

例如 M1 = 20×30 M2 = 25×30 让你有一个类M3的'长相像 M1 + M2 ,一个55×30的矩阵。

当有人问 M3 [28,23] 这个类就知道应该重定向到 M2 [8,23] ,因为货币供应量M1只有20个位置宽(28-20 = 8)。这样,你就不必复制的记忆,这是昂贵的。搞清楚如何重新路由请求权矩阵是便宜得多。取决于有多少矩阵访问之后明显。

修改 这就是我的意思是:

 类节目{
    静态无效的主要(字串[] args){

        INT [,]×= {{1,2,3},{4,5,6}};
        INT [,] Y = {{7,8,9},{10,11,12}};

        变种的xy =新StitchMatrix&其中; INT>(X,Y);

        Console.WriteLine(0,0 =+的xy [0,0]); // 1
        Console.WriteLine(1,1 =+的xy [1,1]); // 5
        Console.WriteLine(1,2 =+的xy [1,2]); // 6
        Console.WriteLine(2,2 =+的xy [2,2]); // 9
        Console.WriteLine(3,2 =+的xy [3,2]); // 12
    }
}

类StitchMatrix< T> {
    私人T [] [,] _matrices;
    私人INT [] _​​lengths;

    公共StitchMatrix(PARAMS T [] [,]矩阵){
        // TODO:检查它们都是相同的大小
        _matrices =矩阵;

        //调用uperbound一次速度
        _lengths = _matrices.Select(米=> m.GetUpperBound(0))的ToArray()。
    }

    公共牛逼这个[INT X,INT Y] {
        得到 {
            //找到合适的基质
            INT iMatrix = 0;
            而(_lengths [iMatrix]其中,X){
                x  -  =(_lengths [iMatrix〕+ 1);
                iMatrix ++;
            }
            //在单元的返回值
            返回_matrices [iMatrix] [X,Y];
        }
    }
}
 

问候格特 - 扬

I have two arrays mat1 & Mat2. I want to have new_mat=[ma1,mat2]; I have written a function which works. I wonder if there is an efficient function for very large matrix or How can I do it with Array.CopyTo method.

public static double[,] Concatenate_matrix_byCol(double[,] Mat1, double[,] Mat2)
{
    int col1=Mat1.GetLength(1);
    int col2 = Mat2.GetLength(1);
    int row1=Mat1.GetLength(0);
    int row2 = Mat2.GetLength(0);
    int i, j,  y;
    double[,] newMat = new double[row1, col1 + col2];

    for (i = 0; i < row1; i++)
    {
        for (j = 0; j < col1; j++)
        {
            newMat[i, j] = Mat1[i, j];
        }
    }                
    for (i = 0; i < row1; i++)
    {
        for (y = 0; y < col2; y++)
        {
            newMat[i, y+col1] = Mat2[i, y];
        }
    }
    return newMat;
}

解决方案

When moving Arrays, you should look into Array.CopyTo instead of moving the cells one by one.

Also you could create a class that accepts the 2 matrices, and provides a level of abstraction that makes them look like 1 matrix but just keeps them seperate underneath.

For instance M1 = 20x 30 and M2 = 25 x 30 so you have a class M3 that 'looks like' M1 + M2, a 55 x 30 matrix.

When someone asks for M3[28, 23] this class will know that it should redirect to M2[8, 23] because M1 was only 20 positions wide (28-20=8). That way you don't have to copy the memory, that's expensive. Figuring out how to reroute a request to the right matrix is much cheaper. Depends on how much the matrix accessed afterwards obviously.

edit This is what I mean:

class Program {
    static void Main(string[] args) {

        int[,] x = { { 1, 2, 3 }, { 4, 5, 6 } };
        int[,] y = { { 7, 8, 9 }, { 10, 11, 12 } };

        var xy = new StitchMatrix<int>(x, y);

        Console.WriteLine("0,0=" + xy[0, 0]); // 1
        Console.WriteLine("1,1=" + xy[1, 1]); // 5
        Console.WriteLine("1,2=" + xy[1, 2]); // 6
        Console.WriteLine("2,2=" + xy[2, 2]); // 9
        Console.WriteLine("3,2=" + xy[3, 2]); // 12
    }
}

class StitchMatrix<T> {
    private T[][,] _matrices;
    private int[] _lengths;

    public StitchMatrix(params T[][,] matrices) {
        // TODO: check they're all same size          
        _matrices = matrices;

        // call uperbound once for speed
        _lengths = _matrices.Select(m => m.GetUpperBound(0)).ToArray();
    }

    public T this[int x, int y] {
        get {
            // find the right matrix
            int iMatrix = 0;
            while (_lengths[iMatrix] < x) {
                x -= (_lengths[iMatrix] + 1);
                iMatrix++;
            }
            // return value at cell
            return _matrices[iMatrix][x, y];
        }
    }
}

Regards Gert-Jan

这篇关于连接一二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆