如何连接矩阵中的多行

发布于 2024-08-25 08:52:42 字数 120 浏览 1 评论 0原文

在Java中,我想将一个数组(a[],固定长度)连接到相同长度的数组,以创建一个矩阵M[2][a的长度]。这样我想随后将更多这些数组粘贴到矩阵上。 (相当于Matlab vertcat函数..C=[A;B]) 这可能吗? 谢谢

In Java I would like to concatenate an array (a[], fixed length) to an array of the same length, to create a matrix M[2][length of a]. This way I would like to subsequently paste more of those arrays onto the matrix. (Comparable to the Matlab vertcat function..C=[A;B])
Is this possible?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失与倦" 2024-09-01 08:52:42

是的,这是可能的。这是一个例子:

public class Main
{
    public static void main(String[] args)
    {
        int[] A = new int[]{1, 2, 3};
        int[] B = new int[]{4, 5, 6};
        int[][] M  = new int[2][];
        M[0] = A;
        M[1] = B;

        for ( int i = 0; i < 2; i ++ ){
             for (int j = 0; j < M[i].length; j++ ){
                 System.out.print(" "+ M[i][j]);
             }
             System.out.println("");
        }
    }
}

上面的打印结果是:

 1 2 3
 4 5 6

不过,我们可以做得更好。如果您使用的是 Java 5 或更高版本,请使用:

public static int[][] vertcat(int[]... args){
   return args;
}

然后您可以编写:

int[][] M = vertcat(A,B);

它适用于任意数量的参数。

编辑
上述方法将原始数组填充到另一个数组中,这意味着对结果的任何修改都会影响原始数组,这可能是不可取的。使用以下命令复制值:

public static int[][] copyMatrix(int[][] original){
    if ( (original==null) || (original.length==0) || (original[0].length == 0) ){
        throw new IllegalArgumentException("Parameter must be non-null and non-empty");
    }
    rows = original.length;
    cols = original[0].length;
    int[][] cpy = new int[rows][cols];
    for ( int row = 0; row < rows; row++ ){
       System.arraycopy(original[row],0,cpy[row],0,cols);
    }
    return cpy;
}

如果您希望 vertcat 返回副本而不是原始值,则可以将其重新定义为:

public static int[][] vertcat(int[]... args){
   return copyMatrix(args);
}

Yes, it is possible. Here is an example:

public class Main
{
    public static void main(String[] args)
    {
        int[] A = new int[]{1, 2, 3};
        int[] B = new int[]{4, 5, 6};
        int[][] M  = new int[2][];
        M[0] = A;
        M[1] = B;

        for ( int i = 0; i < 2; i ++ ){
             for (int j = 0; j < M[i].length; j++ ){
                 System.out.print(" "+ M[i][j]);
             }
             System.out.println("");
        }
    }
}

The above prints out:

 1 2 3
 4 5 6

We can do even better than that, though. If you are using Java 5 or higher, use:

public static int[][] vertcat(int[]... args){
   return args;
}

Then you can write:

int[][] M = vertcat(A,B);

And it will work for any number of arguments.

Edit
The above method stuffs the original arrays into another array, which means that any modification to the result will affect the original arrays, which may be undesireable. Use the following to copy the values:

public static int[][] copyMatrix(int[][] original){
    if ( (original==null) || (original.length==0) || (original[0].length == 0) ){
        throw new IllegalArgumentException("Parameter must be non-null and non-empty");
    }
    rows = original.length;
    cols = original[0].length;
    int[][] cpy = new int[rows][cols];
    for ( int row = 0; row < rows; row++ ){
       System.arraycopy(original[row],0,cpy[row],0,cols);
    }
    return cpy;
}

If you want vertcat to return a copy rather than the original, you can redefine it as:

public static int[][] vertcat(int[]... args){
   return copyMatrix(args);
}
情徒 2024-09-01 08:52:42

据我所知,Java没有内置对矩阵和矩阵相关运算的支持。我要么使用二维数组,编写自己的矩阵包装类(在更简单的情况下),要么寻找一个好的矩阵库(例如 http://jmathtools.berlios.de/)。

As far as I know, Java has no built-in support for matrices and matrix-related operations. I would either use a 2D array, write my own Matrix wrapper class (in simpler cases) or hunt down a good Matrix library (e.g. http://jmathtools.berlios.de/).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文