如何在 Julia 中显示向量和矩阵的所有元素 [英] how to show all elements of vectors and matrices in Julia
问题描述
当数组中有很多元素时,Julia REPL 只显示其中的一部分.例如:
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
有没有办法像上面那样以垂直形式显示所有元素?print(x)
或 showall(x)
把它放在一个丑陋的形式没有换行.
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.
推荐答案
注意:在 0.7 中,Base.STDOUT
已重命名为 Base.stdout
.其余的应该保持不变.---
NOTE: in 0.7, Base.STDOUT
has been renamed to Base.stdout
. The rest should work unchanged.
---
base/中有很多内部使用的方法arrayshow.jl
做与此相关的事情.我找到了
There are a lot of internally used methods in base/arrayshow.jl
doing stuff related to this. I found
Base.print_matrix(STDOUT, x)
工作.可以使用 IOContext
恢复限制行为:
to work. The limiting behaviour can be restored by using an IOContext
:
Base.print_matrix(IOContext(STDOUT, :limit => true), x)
但是,此方法只打印值,而不是包含类型的标头信息.但是我们可以使用 summary
检索该标头(我在查看 这个).
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).
两者结合:
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 导出的...
However, I would wait for some opinions about whether print_matrix
can be relied upon, given that it is not exported from Base...
这篇关于如何在 Julia 中显示向量和矩阵的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!