如何打印Matrix的所有列

时间:2017-04-21 12:28:54

标签: scala apache-spark

我有Matrix,总共包含5列。我想要做的是打印Matrix的所有列,而不仅仅是前两个列,如下所示:

val V: Matrix = svd.V  // The V factor is a local dense matrix.
println(V)

给出以下输出:

-1.0237272594782074E-4  -1.7078345817841522E-4  ... (5 total)
-3.0092323368453486E-4  1.1734582822947035E-4   ...
-8.783338552190558E-4   -0.0017472726007059717  ...
-1.1383724568414156E-4  -4.3548729172213584E-4  ...
-1.1693767421110056E-4  -2.418383762772299E-4   ...
-1.7743361361571285E-4  1.8480473527241232E-4   ...
-1.4886423625353203E-4  2.099922614106897E-4    ...
-8.626317174508992E-4   -9.12157272113119E-4    ...

3 个答案:

答案 0 :(得分:5)

默认的toString方法(使用println时调用)不会显示所有行/列。您应该明确使用另一个{max}和max width作为参数的toString方法:

println(V.toString(5,Int.MaxValue)) //Displays 5 first rows

答案 1 :(得分:0)

将矩阵转换为RDD然后使用foreach应该可以完成工作。

val V: Matrix = svd.V  // The V factor is a local dense matrix.       
val Vrdd = sc.parallelize(V.rowIter.toSeq)
Vrdd.take(8).foreach(println)

将打印前8行和所有列

[-1.023727259478234E-4,-1.7078345817841533E-4,3.0839310929699345E-4,1.2892270831815856E-4,-3.2874234098349364E-4]
[-3.009232336845317E-4,1.173458282294655E-4,2.737768257608553E-4,1.0077068531267255E-5,1.6105536507623856E-4]
[-8.783338552190533E-4,-0.0017472726007059787,9.136695463186932E-4,3.2887292883284746E-4,0.0018690404940346112]
[-1.1383724568414083E-4,-4.354872917221425E-4,-3.621489985336155E-5,2.70169363438746E-5,-5.056654658786913E-4]
[-1.1693767421109712E-4,-2.418383762772343E-4,1.952750723593368E-4,3.5328858284551095E-5,6.579474844822932E-5]
[-1.774336136157171E-4,1.8480473527241484E-4,1.4397978561015781E-6,-1.4225923969798783E-4,-4.2210858440213316E-4]
[-1.4886423625353317E-4,2.0999226141068627E-4,-5.354866549487416E-5,1.2258687855367864E-4,4.4050687210353404E-4]
[-8.626317174509001E-4,-9.121572721131233E-4,-0.004282058263767252,0.004032670016181569,-0.001070431177048715]

答案 2 :(得分:-1)

可能有一种更好的方法,但是嵌套的for循环可以解决这个问题:

for (row <- V) {
    for (column <- row) {
        println(column);
    }
}

研究:https://www.tutorialspoint.com/scala/scala_arrays.htm