如何在Julia中显示向量和矩阵的所有元素 [英] how to show all elements of vectors and matrices in Julia

查看:446
本文介绍了如何在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:

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导出的,因此我将等待有关是否可以依赖print_matrix的一些意见.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆