如何使用库中的函数转置数组?

时间:2013-10-13 18:47:35

标签: java colt

如何使用库中的函数转置数组?我从这里下载并使用了Colt库: http://acs.lbl.gov/software/colt/api/index.html。我试过了:

DoubleMatrix1D array;
array = new DenseDoubleMatrix1D(4);
for (int i=0; i<4; i++)
    array.set(i,i);
DoubleMatrix1D transpose = array.viewDice();

但它不起作用,因为我收到错误:

The method viewDice() is undefined for the type DoubleMatrix1D

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

1D矩阵不包含任何有关它们如何定向的信息。因此,您需要提供此信息才能进行转置。例如,如果使用行向量,则表示有1xm矩阵,因此需要mx1列向量来包含转置。

试试这个:

DoubleMatrix2D transpose = new DenseDoubleMatrix2D(4,1);
for (int i=0; i<4; i++) {
    transpose.setQuick(i,0,array.getQuick(i));
}

如果您有一个列向量,则转置将是一个行向量:

DoubleMatrix2D transpose = new DenseDoubleMatrix2D(1,4);
for (int i=0; i<4; i++) {
    transpose.setQuick(0,i,array.getQuick(i));
}

答案 1 :(得分:0)

这意味着在DoubleMatrix1D类中,方法viewDice()不存在!所以你几乎不能使用它:)。

根据文档,您可以使用:

 double[]   toArray() 
          Constructs and returns a 1-dimensional array containing the cell values.

或许这个:

 DoubleMatrix1D copy() 
          Constructs and returns a deep copy of the receiver.
相关问题