I just found this discussion on SO and it was very helpful: 
http://stackoverflow.com/questions/28271308/avoid-memory-allocation-when-indexing-an-array-in-julia
 


Julia allocates a new array on indexing every time.

Here is how to achieve it if someone is trying to figure the same thing out 
:

julia> a = [1,2,3,4,5,6,7,8,9,0]
10-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6
 7
 8
 9
 0

julia> b = sub(a,[1,4,7])
3-element SubArray{Int64,1,Array{Int64,1},(Array{Int64,1},),0}:
 1
 4
 7

julia> b[1] = 2
2

julia> a
10-element Array{Int64,1}:
 2
 2
 3
 4
 5
 6
 7
 8
 9
 0



सोमवार, 4 मई 2015 को 3:04:53 पूर्व UTC+5:30 को, Maco Anshu ने लिखा:
>
> Hi,
>
> When I index over a Julia array:
>
> julia> x = reshape(1:16, 4, 4)
> 4x4 Array{Int64,2}:
>  1  5   9  13
>  2  6  10  14
>  3  7  11  15
>  4  8  12  16
>
> julia> b =  x[2:3, 2:end-1]
> 2x2 Array{Int64,2}:
>  6  10
>  7  11
>
> julia> b[1,1] = 3
> 3
>
> julia> b
> 2x2 Array{Int64,2}:
>  3  10
>  7  11
>
> julia> x
> 4x4 Array{Int64,2}:
>  1  5   9  13
>  2  6  10  14
>  3  7  11  15
>  4  8  12  16
>
> When does the indexed part of the array gets copied to a new block of 
> memory ? Is b produced as a different array just after indexing or is it 
> copied when one of its elements are changed or does Julia manage this using 
> some other technique ?
> Another question over this is if we have a very huge array (which somehow 
> just fits in RAM), is it possible to do indexing over it without using more 
> and more RAM (avoiding copies) ?
> Something like Python :
> >>> x = np.array([[1,2,3],[4,5,6],[7,8,9]])
> >>> b = x[1:2,:]
> >>> b
> array([[4, 5, 6]])
> >>> b[0] = 0
> >>> x
> array([[1, 2, 3],
>        [0, 0, 0],
>        [7, 8, 9]])
>
> I am not aware of how Julia actually works while indexing and it might be 
> doing something cool like clojure to manage such indexes and avoid copies, 
> so please forgive if the question is really stupid.
>
 

Reply via email to