# How to display the elements of 1D array in one line?

I have an array a,

``````a=Array{Float64,1}()
for i in 1:10
push!(a,i*10.0)
end
display(a)
``````

However, println or display, they all show the elements of the array vertically, like below

10-element Array{Float64,1}:
10.0
20.0
30.0
40.0
50.0
60.0
70.0
80.0
90.0
100.0

1. However, is there a simply command so that it can just show all the elements in one line like

`````` 10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0
``````

and better also can specify the distance between the elements, like the 10(g15.7,1x) in Fortran.

1. Futhermore, if I have another array b which can be any value but still 10 elements. Now I want to display a and b in two lines, but they need to be displayed aligned, like,
println( script for display a)
println ( script for display b )

`````` 10.0      20.0      30.0    40.0    50.0    60.0    70.0    80.0    90.0   100.0
0.10      1205.66   0.25    100.25  57.22   60.23   999.99  777.22  232.12 500.66
``````

is there a way to achieve this?

Edit:

In fact I wanted to display things like below,

``````----- Iteration =  47     -> Log Likelihood =   -5.158964
Sigma =                 0.10265 +-     0.00199
Mixture Label :                 1              |             2              |
Weight =                  0.20000              |       0.80000              |
Mu_k =                  0.63695 +-     0.00333 |     0.30996 +-     0.00484 |
Sig Mu_k =              0.03692 +-     0.00201 |     0.05363 +-     0.00336 |
Mu_V =                 20.01389 +-     0.12476 |    20.01389 +-     0.12476 |
Sig Mu_V =              1.95679 +-     0.08312 |     1.95679 +-     0.08312 |
Mu_k MC acc rate =          2.880%             |         9.941%             |
Mu_V MC acc rate =          5.731%             |         5.731%             |
-----------------------------------------------------------------------------
``````

it is gaussian mixture model, k=1,2.
For each k, I want to show all the results. I want to show them horizontally than vertically.
Because for each k if I need 10 lines, for k=10, if I show them vertically it will take 500 lines which is very inconvenient. But if I show them horizontally, it will only take 10 lines, and it is wide, but is conveneint.
The thing is currently done using Fortran’s format IO, I am still trying to get used to Julia.

``````
julia> join(1:10, "   ")
"1   2   3   4   5   6   7   8   9   10"``````
1 Like

Julia vectors are columns, so it makes sense to print them like columns. If you want to have “row”, you can use a single row matrix `Matrix{Float64}(undef, 1,10)`. You won’t be able to push to it (since single element pushing doesn’t make sense for a matrix), so you’ll have to declare it with `undef` and fill it by indexing.

Not 100% sure what you mean, but are you maybe looking for `range(start=1, step=15.7, length=10)`?

Check out the Printf stdlib. There’s no function that does what you want out of the box, but it should be fairly easy to achieve what you want with that.

2 Likes

If you are just interested in showing this in the REPL for your own use, you can just transpose the vector, and let Julia display it for you:

``````jl> a = collect(10.0:10:100.0);  # no need for that awkward loop

jl> a'
10.0  20.0  30.0  40.0  50.0  60.0  70.0  80.0  90.0  100.0
``````

If you don’t want the header:

``````jl> show(a')
[10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0]
``````

If you also have a vector `b`:

``````jl> b = rand(10) .* 1000;

jl> [a'; b']
2×10 Matrix{Float64}:
10.0   20.0    30.0      40.0  50.0       60.0    70.0      80.0     90.0    100.0
443.6  144.745  63.4774  751.4   2.70519  353.378  13.6564  737.546  644.369  609.264
``````

`show` does not work quite as well here:

``````jl> show([a'; b'])
[10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0; 443.600262923888 144.74517496650608 63.4773542697542 751.4002143975607 2.7051915255487113 353.3781191303855 13.65636167386497 737.5456627385262 644.3692182949352 609.2643705463589]
``````
1 Like

Thank you!
In fact I wanted to display things like below,

``````----- Iteration =  47     -> Log Likelihood =   -5.158964
Sigma =                 0.10265 +-     0.00199
Mixture Label :                 1              |             2              |
Weight =                  0.20000              |       0.80000              |
Mu_k =                  0.63695 +-     0.00333 |     0.30996 +-     0.00484 |
Sig Mu_k =              0.03692 +-     0.00201 |     0.05363 +-     0.00336 |
Mu_V =                 20.01389 +-     0.12476 |    20.01389 +-     0.12476 |
Sig Mu_V =              1.95679 +-     0.08312 |     1.95679 +-     0.08312 |
Mu_k MC acc rate =          2.880%             |         9.941%             |
Mu_V MC acc rate =          5.731%             |         5.731%             |
-----------------------------------------------------------------------------
``````

it is gaussian mixture model, k=1,2.
For each k, I want to show all the results. I want to show them horizontally than vertically.
Because for each k if I need 10 lines, for k=10, if I show them vertically it will take 500 lines which is very inconvenient. But if I show them horizontally, it will only take 10 lines, and it is wide, but is conveneint.
The thing is currently done using Fortran’s format IO, I am still trying to get used to Julia.

Nice Thank you!

I have edited my question. Sorry I always ask my question in a very sloppy way:P
Anyway, thank you very much indeed!