I am writing a program (in .3.1) that takes a one dimensional array of
indices (x) and creates a new one dimensional array of indices (x = y) and
switches a given pair of elements in the new array. For some reason, the
original array (x) changes when I change the new array (see below).
In [16]:
x = [1:5]
y = x
y[[3;4]] = y[[4;3]]
[x y]
Out[16]:
5x2 Array{Int64,2}:
1 1
2 2
4 4
3 3
However, if I change y = x to y = x[:], the problem does not occur, even though
x and x[:] appear to be the same type.
In [15]:
x = [1:5]
y = x[:]
y[[3;4]] = y[[4;3]]
[x y]
Out[15]:
5x2 Array{Int64,2}:
1 1
2 2
3 4
4 3
5 5
The code below shows that this does not seem to be a general property of the
language. As in the previous cases, I define y as x and change y.
In [13]:
x = [1:5]
y = x
y = y*4
[x y]
Out[13]:
5x2 Array{Int64,2}:
1 4
2 8
3 12
4 16
5 20
Certainly, I could use y = x[:] or simply change the original array.
Nonetheless, the inconsistency seems undesirable from my point of view. Is this
a bug? Any help would be appreciated.