如何在Julia

时间:2018-03-15 16:13:05

标签: julia

当我在数组中有许多元素时,Julia REPL只显示其中的一部分。例如:

julia> x = rand(100,2);

julia> x
100×2 Array{Float64,2}:
 0.277023   0.0826133
 0.186201   0.76946  
 0.534247   0.777725 
 0.942698   0.0239694
 0.498693   0.0285596
 ⋮                   
 0.383858   0.959607 
 0.987775   0.20382  
 0.319679   0.69348  
 0.491127   0.976363 

有没有办法像上面那样以垂直形式显示所有元素? print(x)showall(x)将其置于丑陋的形式,没有换行。

2 个答案:

答案 0 :(得分:6)

注意:在0.7中,Base.STDOUT已重命名为Base.stdout。其余的应该保持不变。 ---

base/arrayshow.jl中有很多内部使用的方法来做与此相关的事情。我找到了

Base.print_matrix(STDOUT, x)

上班。使用IOContext

可以恢复限制行为
Base.print_matrix(IOContext(STDOUT, :limit => true), x)

但是,此方法仅打印值,而不打印包含类型的标题信息。但是我们可以使用summary检索该标头(我发现这是this)。

结合两者:

function myshowall(io, x, limit = false) 
  println(io, summary(x), ":")
  Base.print_matrix(IOContext(io, :limit => limit), x)
end

示例:

julia> myshowall(STDOUT, x[1:30, :], true)
30×2 Array{Float64,2}:
 0.21730681784436     0.5737060668051441 
 0.6266216317547848   0.47625168078991886
 0.9726153326748859   0.8015583406422266 
 0.2025063774372835   0.8980835847636988 
 0.5915731785584124   0.14211295083173403
 0.8697483851126573   0.10711267862191032
 0.2806684748462547   0.1663862576894135 
 0.87125664767098     0.1927759597335088 
 0.8106696671235174   0.8771542319415393 
 0.14276026457365587  0.23869679483621642
 0.987513511756988    0.38605250840302996
 ⋮                                       
 0.9587892008777128   0.9823155299532416 
 0.893979917305394    0.40184945077330836
 0.6248799650411605   0.5002213828574473 
 0.13922016844193186  0.2697416140839628 
 0.9614124092388507   0.2506075363030087 
 0.8403420376444073   0.6834231190218074 
 0.9141176587557365   0.4300133583400858 
 0.3728064777779758   0.17772360447862634
 0.47579213503909745  0.46906998919124576
 0.2576800028360562   0.9045669936804894 
julia> myshowall(STDOUT, x[1:30, :], false)
30×2 Array{Float64,2}:
 0.21730681784436     0.5737060668051441 
 0.6266216317547848   0.47625168078991886
 0.9726153326748859   0.8015583406422266 
 0.2025063774372835   0.8980835847636988 
 0.5915731785584124   0.14211295083173403
 0.8697483851126573   0.10711267862191032
 0.2806684748462547   0.1663862576894135 
 0.87125664767098     0.1927759597335088 
 0.8106696671235174   0.8771542319415393 
 0.14276026457365587  0.23869679483621642
 0.987513511756988    0.38605250840302996
 0.8230271471019499   0.37242899586931943
 0.9138200958138099   0.8068913133278408 
 0.8525161103718151   0.5975492199077801 
 0.20865490007184317  0.7176626477090138 
 0.708988887470049    0.8600690517032243 
 0.5858885634109547   0.9900228746877875 
 0.4207526577539027   0.4509115980616851 
 0.26721679563705836  0.38795692270409465
 0.5627701589178917   0.5191793105440308 
 0.9587892008777128   0.9823155299532416 
 0.893979917305394    0.40184945077330836
 0.6248799650411605   0.5002213828574473 
 0.13922016844193186  0.2697416140839628 
 0.9614124092388507   0.2506075363030087 
 0.8403420376444073   0.6834231190218074 
 0.9141176587557365   0.4300133583400858 
 0.3728064777779758   0.17772360447862634
 0.47579213503909745  0.46906998919124576
 0.2576800028360562   0.9045669936804894

然而,我会等待一些关于是否可以依赖print_matrix的意见,因为它不是从Base出口的......

答案 1 :(得分:3)

一个简短/干净的解决方案是

show(stdout, "text/plain", rand(100,2))