how to show all elements of vectors and matrices in Julia

十年热恋 提交于 2020-01-23 16:20:37

问题


When I have many elements in an array, Julia REPL only shows some part of it. For example:

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 

Is there any way to show all elements in the vertical form as above? print(x) or showall(x) put it in an ugly form without line changes.


回答1:


NOTE: in 0.7, Base.STDOUT has been renamed to Base.stdout. The rest should work unchanged. ---

There are a lot of internally used methods in base/arrayshow.jl doing stuff related to this. I found

Base.print_matrix(STDOUT, x)

to work. The limiting behaviour can be restored by using an IOContext:

Base.print_matrix(IOContext(STDOUT, :limit => true), x)

However, this method only prints the values, not the header information containing the type. But we can retrieve that header using summary (which I found out looking at this).

Combining both:

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

Example:

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

However, I would wait for some opinions about whether print_matrix can be relied upon, given that it is not exported from Base...



来源:https://stackoverflow.com/questions/49304329/how-to-show-all-elements-of-vectors-and-matrices-in-julia

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!