juliaprettytable

converting vectors to data structure in Julia


I have 2 vectors of 81 values each. and want to save them in a data structure 9 by 9 so that when I use pretty tables to print it I got the following picture (without converting numbers to string and hard coding "," between them) global c= zeros(81) global c2 = zeros(81)

global data = zip(eachrow(c), eachcol(c2))

I want to create a 9 by 9 data structure using the above code but it gives me error

ERROR: LoadError: The type Base.Iterators.Zip{Tuple{Base.Generator{Base.OneTo{Int64}, Base.var"#239#240"{Vector{Float64}}}, Base.Generator{Base.OneTo{Int64}, Base.var"#241#242"{Vector{Float64}}}}} is not supported. Stacktrace:

enter image description here pretty_table(data ; header = ([-1,-3/4, -1/2, -1/4, 0, 1/4, 1/2, 3/4, 1]), row_names= names)


Solution

  • You can create tuples for each pair of data and print a matrix of those:

    julia> c = rand((-1, -0.1, 0.1, 1), 81); c2 = rand((-1, -0.1, 0.1, 1), 81);
    
    julia> titles = [-1,-3/4, -1/2, -1/4, 0, 1/4, 1/2, 3/4, 1];
    
    julia> data = reshape([tuple(c[i], c2[i]) for i in eachindex(c, c2)], 9, 9)
    9×9 Matrix{Tuple{Real, Real}}:
    ...
    
    julia> pretty_table(data; header = titles, row_names = titles)
    ┌───────┬─────────────┬───────────┬─────────────┬──────────────┬─────────────┬─────────────┬─────────────┬─────────────┬──────────────┐
    │       │        -1.0 │     -0.75 │        -0.5 │        -0.25 │         0.0 │        0.25 │         0.5 │        0.75 │          1.0 │
    ├───────┼─────────────┼───────────┼─────────────┼──────────────┼─────────────┼─────────────┼─────────────┼─────────────┼──────────────┤
    │  -1.0 │  (-0.1, -1) │    (1, 1) │  (-0.1, -1) │     (0.1, 1) │    (0.1, 1) │     (-1, 1) │  (0.1, 0.1) │ (-0.1, 0.1) │    (-0.1, 1) │
    │ -0.75 │  (0.1, 0.1) │ (1, -0.1) │    (0.1, 1) │     (1, 0.1) │    (0.1, 1) │  (0.1, 0.1) │   (0.1, -1) │   (-0.1, 1) │    (-0.1, 1) │
    │  -0.5 │    (-1, -1) │   (-1, 1) │     (1, -1) │    (0.1, -1) │   (-1, 0.1) │   (1, -0.1) │   (-1, 0.1) │ (0.1, -0.1) │   (-1, -0.1) │
    │ -0.25 │ (-0.1, 0.1) │ (-0.1, 1) │  (-1, -0.1) │     (-1, -1) │      (1, 1) │   (-1, 0.1) │  (-0.1, -1) │   (1, -0.1) │ (-0.1, -0.1) │
    │   0.0 │     (-1, 1) │    (1, 1) │    (1, 0.1) │    (0.1, -1) │    (0.1, 1) │ (-0.1, 0.1) │  (0.1, 0.1) │  (-0.1, -1) │   (-1, -0.1) │
    │  0.25 │  (0.1, 0.1) │   (-1, 1) │   (1, -0.1) │     (1, 0.1) │   (1, -0.1) │  (0.1, 0.1) │     (-1, 1) │      (1, 1) │     (1, 0.1) │
    │   0.5 │     (-1, 1) │   (-1, 1) │   (1, -0.1) │   (-0.1, -1) │ (-0.1, 0.1) │   (-0.1, 1) │     (-1, 1) │   (0.1, -1) │   (-1, -0.1) │
    │  0.75 │     (-1, 1) │  (1, 0.1) │    (0.1, 1) │     (1, 0.1) │   (1, -0.1) │ (0.1, -0.1) │ (0.1, -0.1) │  (-1, -0.1) │   (0.1, 0.1) │
    │   1.0 │   (0.1, -1) │  (-1, -1) │ (-0.1, 0.1) │ (-0.1, -0.1) │ (-0.1, 0.1) │      (1, 1) │  (-0.1, -1) │   (-0.1, 1) │      (-1, 1) │
    └───────┴─────────────┴───────────┴─────────────┴──────────────┴─────────────┴─────────────┴─────────────┴─────────────┴──────────────┘