如何打印 JAMA 矩阵的列?

发布于 2024-07-14 16:06:51 字数 34 浏览 7 评论 0原文

我使用 JAMA.matrix 包..如何打印矩阵的列

I use the JAMA.matrix package..how do i print the columns of a matrix

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

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

发布评论

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

评论(3

在风中等你 2024-07-21 16:06:52

最简单的方法可能是 转置矩阵,然后打印每一行。 取 API 中的示例的一部分:

double[][] vals = {{1.,2.,3},{4.,5.,6.},{7.,8.,10.}};
Matrix a = new Matrix(vals);
Matrix aTransposed = a.transpose();
double[][] valsTransposed = aTransposed.getArray();

// now loop through the rows of valsTransposed to print
for(int i = 0; i < valsTransposed.length; i++) {
    for(int j = 0; j < valsTransposed[i].length; j++) {        
        System.out.print( " " + valsTransposed[i][j] );
    }
}

正如 duffymo 指出的在注释中,绕过转置并仅编写嵌套的 for 循环来打印列而不是跨行会更有效。 如果您需要两种方式打印,则会导致代码量增加一倍。 这是一个足够常见的权衡(速度与代码大小),我将其留给您来决定。

The easiest way would probably be to transpose the matrix, then print each row. Taking part of the example from the API:

double[][] vals = {{1.,2.,3},{4.,5.,6.},{7.,8.,10.}};
Matrix a = new Matrix(vals);
Matrix aTransposed = a.transpose();
double[][] valsTransposed = aTransposed.getArray();

// now loop through the rows of valsTransposed to print
for(int i = 0; i < valsTransposed.length; i++) {
    for(int j = 0; j < valsTransposed[i].length; j++) {        
        System.out.print( " " + valsTransposed[i][j] );
    }
}

As duffymo pointed out in a comment, it would be more efficient to bypass the transposition and just write the nested for loops to print down the columns instead of across the rows. If you need to print both ways that would result in twice as much code. That's a common enough tradeoff (speed for code size) that I leave it to you to decide.

醉态萌生 2024-07-21 16:06:52

您可以在矩阵上调用 getArray() 方法来获取表示元素的 double[][]。
然后您可以循环该数组以显示您想要的任何列/行/元素。

有关更多方法,请参阅 API

You can invoke the getArray() method on the matrix to get a double[][] representing the elements.
Then you can loop through that array to display whatever columns/rows/elements you want.

See the API for more methods.

装纯掩盖桑 2024-07-21 16:06:52
public static String strung(Matrix m) {
    StringBuffer sb = new StringBuffer();
    for (int r = 0; r < m.getRowDimension(); ++ r) {
        for (int c = 0; c < m.getColumnDimension(); ++c)
            sb.append(m.get(r, c)).append("\t");
        sb.append("\n");
    }
    return sb.toString();
}
public static String strung(Matrix m) {
    StringBuffer sb = new StringBuffer();
    for (int r = 0; r < m.getRowDimension(); ++ r) {
        for (int c = 0; c < m.getColumnDimension(); ++c)
            sb.append(m.get(r, c)).append("\t");
        sb.append("\n");
    }
    return sb.toString();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文