Arrays vs Vectors vs matrix (permutations) problems,confusion and clarification

Hi,

here is what’s confuse me:

a=[1,3,5]
b=[2,4,6]

[a b]
3×2 Array{Int64,2}:
 1  2
 3  4
 5  6

reshape([1 2 3 4 5 6],2,3)
2×3 Array{Int64,2}:
 1  3  5
 2  4  6

[[1 2], [3 4], [5 6]]
Matrix{Int64}[3]
1×2 Array{Int64,2}:
 1  2
1×2 Array{Int64,2}:
 3  4
1×2 Array{Int64,2}:
 5  6

[[1,2], [3,4], [5,6]]
Vector{Int64}[3]
Int64[2]
1
2
Int64[2]
3
4
Int64[2]
5
6

We can see they are all different but some are the same

Array{Int64, 1} == Vector{Int64}
true

Array{Int64, 2} == Matrix{Int64}
true

Now I am trying to use permutations and I got some strange stuff:

[a b][[1 3 2]]
1×3 Array{Int64,2}:
 1  5  3

[a b][[1,3,2]]
Int64[3]
1
5
3

[[1 2], [3 4], [5 6]][[1 3 2]]
1×3 Array{Array{Int64,2},2}:
 [1 2]  [5 6]  [3 4]

[[1 2], [3 4], [5 6]][[1,3,2]]
Matrix{Int64}[3]
1×2 Array{Int64,2}:
 1  2
1×2 Array{Int64,2}:
 5  6
1×2 Array{Int64,2}:
 3  4

[[1,2], [3,4], [5,6]][[1 3 2]]
1×3 Array{Array{Int64,1},2}:
 [1, 2]  [5, 6]  [3, 4]

[[1,2], [3,4], [5,6]][[1,3,2]]
Vector{Int64}[3]
Int64[2]
1
2
Int64[2]
5
6
Int64[2]
3
4

So I am trying to applies the permutation [1,3,2] (switching the two last elements) and we clearly see that my way of doing it doesn’t work with the “Array type” but only with the Matrix and Vector type.

Is that a normal behavior that this way of permuting doesn’t work with the Array type when the dim is > 1 and return instead a 1dim object?

Is there a fast way to convert those type from one to another?

Is there a better way to applies permutations to Array type?

Is there any function similar the (hcat,vcat,…) but who return a Matrix or a Vector type?

thx you

Maybe I am just confuse between

3×2 Array{Int64,2}:

and

1×3 Array{Array{Int64,2},2}:

and

Matrix{Int64}[3]
1×2 Array{Int64,2}:
1×2 Array{Int64,2}:
1×2 Array{Int64,2}:

the fist type is the only one who doesn’t accept the way I try to permute.

how do I create the second one or third one with vcat or other from two “Int64” vectors.

i.e:
x = rand(0:20,4)
y = rand(0:20,4)

thank you to in-light me :slight_smile:

This is how it works at least on v0.6.3. Can you tell us which version of Julia are you using?

[a b] # hcat
[a; b] # vcat
[a, b] # 1-dim array (vertical vector) constructor independently on what a and b are

Some of your examples don’t fit into this criteria, and hence I cannot reproduce them. Pleas copy paste the outputs instead of writing what you expect.

[UPDATE]
Then when you call the elements by indexing you can use a vector (or 1-dim array) for the n-th dimension of your array.

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

julia> A[5] # get the 5th element in column major order 
4

julia> A[1, 2] # get by 2-d indexing
2

julia> A[[1,3], 2] # choose 2 elements with the 2nd index in common, or run the first index in [1,3]
2-element Array{Int64,1}:
 2
 6

julia> A[[1,3,2], 2] # the same which is equivalent to permute the 2nd column with the permute vector [1,3,2]
3-element Array{Int64,1}:
 2
 6
 4

julia> A[[1,3,2], [2,1]] # now permute even with the second index
3×2 Array{Int64,2}:
 2  1
 6  5
 4  3
1 Like

I am using juliapro the VERSION command return v"0.6.2".

These are the exact output I copy past from the Atom interface.

a=[1,3,5]
b=[2,4,6]
[a b]
reshape([1 2 3 4 5 6],2,3)
[[1 2], [3 4], [5 6]]
[[1,2], [3,4], [5,6]]
[a b][[1 3 2]]
[a b][[1,3,2]]
[[1 2], [3 4], [5 6]][[1 3 2]]
[[1 2], [3 4], [5 6]][[1,3,2]]
[[1,2], [3,4], [5,6]][[1 3 2]]
[[1,2], [3,4], [5,6]][[1,3,2]]

Here is all the codes without output.
Let me know witch output are different for you.

Bonus

[[1 2], [3 4], [5 6]][[1;3;2]] # take 1st, 3rd, and 2nd elements and vcat them

I think what you’re primarily missing is

julia> [a b][[1,3,2], :]
3×2 Array{Int64,2}:
 1  2
 5  6
 3  4

Depending on what you want to do nested arrays could be useful but you’re probably best off working with a matrix.

Nice @Iagoba_Apellaniz I understand much more the multidim Arrays with all your explanations.
Basically I was looking for A[[1,3,2], [1,2]] when I was writing A[[1,3,2]] witch is A[[1,3,2], 1].
Now @GunnarFarneback say you can write it like this A[[1,3,2], :] can you explain the :?
since I am starting from x and y coordinates.

xval = rand(0:20,4)
yval = rand(0:20,4)

I rather have nested arrays so I can use the [x,y] points separately but I am not sure how to use hcat to accomplish that.

thank you very much

The : means all indices along that dimension.

If you really want nested arrays you can construct them with a comprehension, e.g.

julia> xval = rand(0:20,4)
4-element Array{Int64,1}:
  3
 12
  7
 19

julia> yval = rand(0:20,4)
4-element Array{Int64,1}:
 12
  1
  6
 14

julia> [vcat(p...) for p in zip(xval, yval)]
4-element Array{Array{Int64,1},1}:
 [3, 12] 
 [12, 1] 
 [7, 6]  
 [19, 14]
1 Like

Or if you want to be less fancy,

julia> [[xval[k], yval[k]] for k = 1:length(xval)]
4-element Array{Array{Int64,1},1}:
 [3, 12] 
 [12, 1] 
 [7, 6]  
 [19, 14]

You can use ranges for the indices of a multi-dimensional array

julia> A = [1 2 3; 4 5 6; 7 8 9]
3×3 Array{Int64,2}:
 1  2  3
 4  5  6
 7  8  9

julia> A[1:2, 2:3] 
2×2 Array{Int64,2}:
 2  3
 5  6

You can even use the end keyword to say ‘last’. Hence,

julia> A[1:2, 2:end] 
2×2 Array{Int64,2}:
 2  3
 5  6

Finally, if you want something in 1:end you can use simply the colon

julia> A[1:2, 1:end]
2×3 Array{Int64,2}:
 1  2  3
 4  5  6

julia> A[1:2, :]
2×3 Array{Int64,2}:
 1  2  3
 4  5  6

I recommend you to read the docs for multi-dim arrays in v0.6. And don’t forget that soon will be the release of Julia v1.0, and then I would also recommend you to stay tuned for the last changes.

starting from:

x = rand(0:20,4)
y = rand(0:20,4)

After trying those:

@time [[i...] for i in zip(x,y)]
@time [collect(i) for i in zip(x,y)]
@time diag([[i,j] for i in x,j in y])
@time [[x[k], y[k]] for k = 1:length(x)]
@time [hcat(p...) for p in zip(x, y)]

the most efficient way (for little vectors at least) is:

[collect(i) for i in zip(x,y)]
Vector{Int64}[4]
Int64[2]
Int64[2]
Int64[2]
Int64[2]

@GunnarFarneback my output is a bit different from yours , is it because our version of Julia is different?

(4-element Array{Array{Int64,1},1}: vs Vector{Int64}[4])

thx again