Access jth component of stacked 1D array


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?


Nx = 50
Ne = 20

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

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

You can broadcast getindex.(A,j)


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.

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

using LazyArrays

using LazyStack

using JuliennedArrays

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

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

Nx = 50
Ne = 20

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

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

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]   

The following function seems to work:

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)

  Ne = 10
A = [[Ne-i+2; i+5.0; i] for i=1:Ne]

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.