Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
filtering arrays
#1
Hi everyone,

This is my first post in this forum. This question is not related with homework but it is really basic question. So I hope this is the correct section. The other day I was filtering some data using this similar routine

A=X
A[A==0]=numpy.nan
I did this to prevent the modification of the ndarray X. The interesting part is my array X was also modified!! I am not sure why. Thank you so much in advance for your help

Kind regards,

Estanislao

PD: I used python 3.7
Reply
#2
A==0 resolves to True or False, True & False are also represented as 0 & 1
A[A==0]=numpy.nan
will change value at the index of 0 or 1 depending on the outcome of A==0

Edit: may have gone off in a tangent
A=X
A is now pointing to the same object that X is
you would need a copy of x, presuming x is a list
A=X[:]
Reply
#3
Hi Yoriz,

Yes, I know how it works that command. I used it a lot in MATLAB. My question is why that command modified the array A and the array X. I would have expected that only the array A is modified.

Regards,
Reply
#4
See the additional edit about copying in the above post
numpy.copy
Reply
#5
Hi Yoriz,

Thank you so much. That was extremely helpful. I did not know the existence of that function.

Regards,
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020