# Access jth component of stacked 1D array

**URL:** <https://discourse.julialang.org/t/access-jth-component-of-stacked-1d-array/41461>\
**Category:** General Usage\
**Created:** [June 15, 2020, 7:03pm UTC](https://discourse.julialang.org/t/access-jth-component-of-stacked-1d-array/41461 "2020-06-15T19:03:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [June 15, 2020, 7:03pm UTC](https://discourse.julialang.org/t/access-jth-component-of-stacked-1d-array/41461/1 "2020-06-15T19:03:26Z")

</div>

Hello,

I would like to access the jth component of stacked 1D arrays, what is the best option? Is it possible to create a view of this subarray?

e.g.:

```julia
Nx = 50
Ne = 20

A = map(i->randn(Nx,1:Ne)

# Access jth component
j = 10
[A[i][j] for i=1:Ne]

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [June 15, 2020, 7:08pm UTC](https://discourse.julialang.org/t/access-jth-component-of-stacked-1d-array/41461/2 "2020-06-15T19:08:54Z")

</div>

You can broadcast `getindex.(A,j)`

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 15, 2020, 7:55pm UTC](https://discourse.julialang.org/t/access-jth-component-of-stacked-1d-array/41461/3 "2020-06-15T19:55:44Z")

</div>

However, if what you want is a `view` in the sense that you can replace an element in this new array `B` to replace an element of a vector in `A` (i.e., `B[i] = x` reflects in `A[i][j]` value) then this will not work. You can broadcast `setindex` too if necessary.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [June 15, 2020, 8:13pm UTC](https://discourse.julialang.org/t/access-jth-component-of-stacked-1d-array/41461/4 "2020-06-15T20:13:37Z")

</div>

You can try to concatenate views into one, using any of these… only the last seems to allow `setindex!`:

```julia
using LazyArrays
Vcat(view.(A,1))

using LazyStack
stack(view.(A,1))

using JuliennedArrays
Align(view.(A,Ref(1:1)),2)

```

But it might be easier to work with a matrix, not a vector of vectors.

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [June 15, 2020, 9:45pm UTC](https://discourse.julialang.org/t/access-jth-component-of-stacked-1d-array/41461/5 "2020-06-15T21:45:30Z")

</div>

Thank you for your answers,

A related question:  
Can we sort in-place component by component an array of 1D array?

Here is my code but it creates an extra array

```julia
Nx = 50
Ne = 20

A = map(i->randn(Nx,1:Ne))
B =sort!(hcat(A...),dims = 2)
A = map(i-> B[:,i],1:Ne)
```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [June 15, 2020, 10:14pm UTC](https://discourse.julialang.org/t/access-jth-component-of-stacked-1d-array/41461/6 "2020-06-15T22:14:06Z")

</div>

Note that you’re missing a bracket, your code doesn’t run. But sure, you can do this:

```julia
julia> A = [rand(Int8,3) for _ in 1:5]
5-element Array{Array{Int8,1},1}:
 [17, 2, 83]     
 [65, -71, -30]  
 [-119, -127, -9]
 [-90, 22, 122]  
 [22, -26, 63]   

julia> sort!(Align(A,2), dims=1);

julia> A
5-element Array{Array{Int8,1},1}:
 [-119, -127, -30]
 [-90, -71, -9]   
 [17, -26, 63]    
 [22, 2, 83]      
 [65, 22, 122]   

```

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [June 15, 2020, 11:07pm UTC](https://discourse.julialang.org/t/access-jth-component-of-stacked-1d-array/41461/7 "2020-06-15T23:07:04Z")

</div>

The following function seems to work:

```julia
import Base: sort!

function sort!(A::Array{Array{Float64,1},1})
    Nx = size(A[1],1)
    for i = 1:Nx
        setindex!.(A,sort!(getindex.(A,i)), i)
    end
end

  Ne = 10
A = [[Ne-i+2; i+5.0; i] for i=1:Ne]
sort!(A)
A

```

Thank you for the reference to package for lazy arrays. What is the advantage to use lazy arrays compared to regular arrays?

I don’t know how to create a view that contains the first N components of each array.
