How to force single column array?

Thanks! Will try it out and read a bit of documentation and will come back again if I still don’t get it or got it to work as I want.

Got it, it works now! Thanks for your time and same to everybody else who helped me out :slight_smile:

My final question is whether there is an easy way to “unpack” the data? Since now when I run the command for let us say positions I get:

a[1]
125751-element Array{SArray{Tuple{3},Float32,1,3},1}:
 [0.0, 0.0, 0.0]
 [0.0, 0.0, 0.02]
 [0.0, 0.0, 0.04]
 [0.02, 0.0, 0.0]
 [0.04, 0.0, 0.0]
 [0.06, 0.0, 0.0]
 [0.08, 0.0, 0.0]
 [0.1, 0.0, 0.0]
 [0.12, 0.0, 0.0]
 [0.14, 0.0, 0.0]
 [0.16, 0.0, 0.0]
 [0.18, 0.0, 0.0]
 [0.2, 0.0, 0.0]

Which is great, since now every particle has a vector assigned with its relevant positions as we talked about - in my former way of doing it though, I could easily make a scatter plot of the positions using:

scatter(a[1][:,1],a[1][:,3])

Since it was in matrix form and not vector form - is there a simple way to “unpack” all the vectors so they become a “Array{Float32,2}” again? I can do it easily with a for loop, I just want to make sure I don’t miss anything basic.Just to provide an example for what I mean with unpacked data which made it easy to use scatter command:

2×3 Array{Float32,2}:
 0.0  0.0  0.0
 0.0  0.0  0.2
 0.0 0.0 0.04
...

And as you said I saw a small performance increase and I am sure it is going to be magnified when I start doing calculations on the vectors down the line :slight_smile:

Kind regards

You could probably use RecursiveArrayTools.jl for this. Try v = VectorOfArray(a[1]) which should then be indexable like a matrix.

Yes, you can use reinterpret(Float32, x) to interpret a vector of SVectors as a matrix.

1 Like

Err, sorry, I think that will actually give you a flat vector, not a matrix. I’m not at a computer right now, but I think you want reshape(reinterpret(Float32, x), 3, :)

Thanks your solution works for my purpose.

I might try looking at @carstenbauer if I need to do it more often in the future.

Thanks for all the help in this thread.

The reinterpret approach is the best if you are going to be doing anything else with the data but if you just want to do things like

then a broadcasted getindex works well

scatter(getindex.(a[1], 1), getindex.(a[1], 3))
2 Likes

Thanks!

Here’s also my way:
a=Array{Int32}(collect(zeros(4)))
or

b=4
a=Array(collect(zeros(b)))

or maybe:

b=rand(5,6)
a=Array(vcat(b...))

Just write

a = zeros(Int32, 4)

No need to collect zeros, it’s already collected. And no point in wrapping it in Array, it’s already an array.

This is just

a = vec(b)

Don’t use splatting for something like this, it is super slow:

julia> @btime vec($b);
  35.559 ns (2 allocations: 80 bytes)

julia> @btime Array(vcat($b...));
  902.184 ns (32 allocations: 1.13 KiB)
2 Likes

That’s probably due to my tuple into array convert practice, but in this example it works like you said.

If you really need to turn an iterable into a vector (but you should very rarely do that) then try to use collect first. If it’s a tuple, collect will turn it into a vector. There’s no need to splat and vcat and then wrap it in an Array call on top of everything else. Just a simple collect(b) will do.

If your code looks complicated, then you are probably overcomplicating it :wink:

1 Like