简体   繁体   中英

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))

在此处输入图像描述 pretty_table(data; header = ([-1,-3/4, -1/2, -1/4, 0, 1/4, 1/2, 3/4, 1]), row_names= names)

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) │
└───────┴─────────────┴───────────┴─────────────┴──────────────┴─────────────┴─────────────┴─────────────┴─────────────┴──────────────┘

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM