0

I have an array A ([1,2,3,4]) and want to define an array B with the first element the same as in A, while the rest are in the reversed order of the rest in A, ie. B=[1,4,3,2]. What I did was

A=[1,2,3,4]
B=A
C=A[1:4]
B[1:4]=C[::-1]

After executing this code, A became [1,4,3,21!! What's the problem here? How to keep the original array A?

Thanks in advance!

amitchone
  • 1,630
  • 3
  • 21
  • 45
open0121
  • 105
  • 2
  • 15
  • `B=A` creates another reference to exactly the same list, not a brand new list. – John Coleman Nov 12 '18 at 12:44
  • Try This: `B = [A[0]]+ A[1:][::-1]` – Rohit-Pandey Nov 12 '18 at 12:44
  • Thanks John and Rohit! I didn't know that it was totally different from Matlab:). In my case array A is a column vector with the first element '0', by executing B = [A[0]]+ A[1:][::-1], I missed the first element in A (i.e. B became a column vector with one less element than A...). What's the proper way to treat column vector? – open0121 Nov 12 '18 at 15:10

0 Answers0