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

**URL:** <https://discourse.julialang.org/t/arrays-vs-vectors-vs-matrix-permutations-problems-confusion-and-clarification/11578>\
**Category:** General Usage\
**Created:** [June 11, 2018, 8:48am UTC](https://discourse.julialang.org/t/arrays-vs-vectors-vs-matrix-permutations-problems-confusion-and-clarification/11578 "2018-06-11T08:48:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rapasite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapasite/32/3719_2.png) [@rapasite](https://discourse.julialang.org/u/rapasite)\
**Post date:** [June 11, 2018, 8:48am UTC](https://discourse.julialang.org/t/arrays-vs-vectors-vs-matrix-permutations-problems-confusion-and-clarification/11578/1 "2018-06-11T08:48:55Z")

</div>

Hi,

here is what’s confuse me:

```julia
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

```julia
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:

```julia
[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

---

<div class="post-metadata">

**Author:** ![rapasite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapasite/32/3719_2.png) [@rapasite](https://discourse.julialang.org/u/rapasite)\
**Post date:** [June 11, 2018, 9:03am UTC](https://discourse.julialang.org/t/arrays-vs-vectors-vs-matrix-permutations-problems-confusion-and-clarification/11578/2 "2018-06-11T09:03:48Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Iagoba\_Apellaniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iagoba_apellaniz/32/34406_2.png) [@Iagoba\_Apellaniz](https://discourse.julialang.org/u/Iagoba_Apellaniz)\
**Post date:** [June 11, 2018, 9:04am UTC](https://discourse.julialang.org/t/arrays-vs-vectors-vs-matrix-permutations-problems-confusion-and-clarification/11578/3 "2018-06-11T09:04:17Z")

</div>

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

```julia
[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
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
```

---

<div class="post-metadata">

**Author:** ![rapasite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapasite/32/3719_2.png) [@rapasite](https://discourse.julialang.org/u/rapasite)\
**Post date:** [June 11, 2018, 9:12am UTC](https://discourse.julialang.org/t/arrays-vs-vectors-vs-matrix-permutations-problems-confusion-and-clarification/11578/4 "2018-06-11T09:12:55Z")

</div>

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

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![Iagoba\_Apellaniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iagoba_apellaniz/32/34406_2.png) [@Iagoba\_Apellaniz](https://discourse.julialang.org/u/Iagoba_Apellaniz)\
**Post date:** [June 11, 2018, 9:27am UTC](https://discourse.julialang.org/t/arrays-vs-vectors-vs-matrix-permutations-problems-confusion-and-clarification/11578/5 "2018-06-11T09:27:35Z")

</div>

> [@rapasite](#):
>
> ```julia
> a=[1,3,5] # 1-dim vertical vector, most used array, that's why is defined with commas
> b=[2,4,6] # One can do instead vcat to all 3 integers as [2; 4; 6] 
> [a b] # hcat the 2 vertical vectors
> reshape([1 2 3 4 5 6],2,3) # reshape the 6 element that where constructed with hcat
> [[1 2], [3 4], [5 6]] # construct an array (1-dim vector) with three different [n m] (hcat 2 integers)
> [[1,2], [3,4], [5,6]] # construct an array (1-dim vector) with three different [n, m] (1-dim arrays of 2 integers)
> [a b][[1 3 2]] # this is weird to me since permutes it and then uses hcat
> [a b][[1,3,2]] # permutes or constructs a 1-dim vector with the 1st, 3rd, and 2nd elements
> [[1 2], [3 4], [5 6]][[1 3 2]] # again, permute and hcat
> [[1 2], [3 4], [5 6]][[1,3,2]] # permutes the 2-element horizontal-vectors inside the 1-dim array
> [[1,2], [3,4], [5,6]][[1 3 2]] # again, permute and hcat
> [[1,2], [3,4], [5,6]][[1,3,2]] # permutes or constructs a 1-dim vector with the 1st, 3rd, and 2nd elements, in this case [n, m]
> 
> ```

Bonus

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

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 11, 2018, 9:33am UTC](https://discourse.julialang.org/t/arrays-vs-vectors-vs-matrix-permutations-problems-confusion-and-clarification/11578/6 "2018-06-11T09:33:08Z")

</div>

I think what you’re primarily missing is

```julia
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.

---

<div class="post-metadata">

**Author:** ![rapasite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapasite/32/3719_2.png) [@rapasite](https://discourse.julialang.org/u/rapasite)\
**Post date:** [June 11, 2018, 9:48am UTC](https://discourse.julialang.org/t/arrays-vs-vectors-vs-matrix-permutations-problems-confusion-and-clarification/11578/7 "2018-06-11T09:48:40Z")

</div>

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.

```julia
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

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 11, 2018, 9:58am UTC](https://discourse.julialang.org/t/arrays-vs-vectors-vs-matrix-permutations-problems-confusion-and-clarification/11578/8 "2018-06-11T09:58:58Z")

</div>

The `: ` means all indices along that dimension.

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

```julia
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]

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 11, 2018, 10:01am UTC](https://discourse.julialang.org/t/arrays-vs-vectors-vs-matrix-permutations-problems-confusion-and-clarification/11578/9 "2018-06-11T10:01:26Z")

</div>

Or if you want to be less fancy,

```julia
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]

```

---

<div class="post-metadata">

**Author:** ![Iagoba\_Apellaniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iagoba_apellaniz/32/34406_2.png) [@Iagoba\_Apellaniz](https://discourse.julialang.org/u/Iagoba_Apellaniz)\
**Post date:** [June 11, 2018, 10:01am UTC](https://discourse.julialang.org/t/arrays-vs-vectors-vs-matrix-permutations-problems-confusion-and-clarification/11578/10 "2018-06-11T10:01:38Z")

</div>

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

```julia
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
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
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](https://docs.julialang.org/en/release-0.6/manual/arrays/). 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.

---

<div class="post-metadata">

**Author:** ![rapasite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapasite/32/3719_2.png) [@rapasite](https://discourse.julialang.org/u/rapasite)\
**Post date:** [June 11, 2018, 8:27pm UTC](https://discourse.julialang.org/t/arrays-vs-vectors-vs-matrix-permutations-problems-confusion-and-clarification/11578/11 "2018-06-11T20:27:56Z")

</div>

starting from:

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

```

After trying those:

```julia
@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:

```julia
[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?

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

```

thx again
