Displaying column data as columns?

A is a 1-column data with 3 elements:
A = [1, 2, 3, 4, 5];

Right now, if do the below:
@show A;

The below will be shown on my REPL:
A = [1, 2, 3, 4, 5];

Is there a way I can set up Julia, so that it will display column data as columns, just like what Matlab is doing? For example, in this case, I would see the below:

A = 
1
2
3
4
5

Thanks.

julia> A = [1, 2, 3, 4, 5];

julia> A
5-element Vector{Int64}:
 1
 2
 3
 4
 5

also

julia> display(A)
5-element Vector{Int64}:
 1
 2
 3
 4
 5
1 Like

Many thanks!

Maybe that is not a good example. Sometimes the output of my program will display column data as if they are rows, which I find very confusing.

One of the other things the output does is (wisely) truncate the middle of a long array when printing. But also sometimes, if I have a DataFrame with 60 columns and do names(df) I want all 60 names so I can scroll through them.

Quick & dirty would be:

map(println, A);

The semi-colon is so you also don’t print the result of map which is one nothing for evey element

Instead of using a map with ;, you should be just using:

foreach(println, A)

Which is a map that always returns nothing (does not save the values to arrays).

For displaying in a single line without a limit on the number of elements you can do: show(IOContext(stdout, :limit => false), A).

4 Likes

β€œQuick & dirty would be”

for some values of β€œshould”. foreach is 3 more letters to type

and the second one even longer

1 Like

For some values of β€œquick”: map is allocating a whole new vector even if you are not displaying it, XD.

Also, you can be sure I will forget about the ; enough times to just be faster to type foreach instead of map + ;.

3 Likes

You don’t need to worry about row vs column vectors in Julia as you do in Matlab. A Julia Vector is neither a row nor a column because it is truly 1-Dimensional and does not know about any other dimensions to orient itself against.

If you slice a Matrix in Julia in either direction, the result is a 1D Vector that forgets about its previous orientation.

julia> A = [1 2 3; 4 5 6; 7 8 9]
3Γ—3 Matrix{Int64}:
 1  2  3
 4  5  6
 7  8  9

julia> A[1,:]
3-element Vector{Int64}:
 1
 2
 3

julia> A[:,1]
3-element Vector{Int64}:
 1
 4
 7

I can think of two examples where a row is strictly treated as a row:

  1. Plots.jl will sometimes ask for a 1x? Matrix as input rather than a Vector. This looks like a Matlab row vector but is really a special case of the 2D Matrix data type.
julia> A = [1 2 3 4 5]
1Γ—5 Matrix{Int64}:
1  2  3  4  5
  1. DataFrames.jl has a DataFrameRow type. It is used to preserve extra information about the data frame that would be lost if the data was presented as a Vector. For example, it has the special ability to be indexed by column name.
julia> using DataFrames

julia> df = DataFrame(A=[1,2,3],B=[4,5,6])
3Γ—2 DataFrame
 Row β”‚ A      B     
     β”‚ Int64  Int64 
─────┼──────────────
   1 β”‚     1      4
   2 β”‚     2      5
   3 β”‚     3      6

julia> df[:,1]
3-element Vector{Int64}:
 1
 2
 3

julia> df[1,:]
DataFrameRow
 Row β”‚ A      B     
     β”‚ Int64  Int64
─────┼──────────────
   1 β”‚     1      4

julia> df[1,:]["B"]
4
2 Likes

I came across this today

julia> using TerminalPager
julia> pager(rand(100))
100-element Vector{Float64}:
 0.3744263500648872
 0.13999473684421915
 0.27656898057508084
 0.8730220303287213
 0.3426440636338286
 0.8960220731666186
 0.396795813237402
 0.8495009604983786
 0.3469971171086582
 0.04243121442940978
 0.10625742146140316
 0.05642715944299159
 0.20889060271420368
:

and the colon at the end is waiting for space/enter or q like more(1)