How can I manipulate a copy of an array without changing the original array?

plshelp

I have an array called n with numbers, and my goal is to make an array m where m[i] = n[i] - n[i-1]. m[0] is just equal to n[0]. I've tried this:

import numpy as np
n = np.array([1,2,3,4])
m = n
for i in range(1, len(n)):
    m[i] = n[i] - n[i-1]

The assignment in the for loop does something I don't understand, because it makes both n and m into arrays = [1 1 2 2]. I simply want to change the inputs in m.

Note: My code does as I want it to when I strictly initialize both n and m like this:

n = np.array([1,2,3,4])
m = np.array([1,2,3,4])

But I feel like I should be able to make a copy of n and be able to to manipulate ONLY the copy. Any suggestions or help?

Talendar

By doing m = n, you're simply binding a new name m to an existing array named n. That's why, when you later make changes to m, you see the same changes applied to n. They both refer to the same object in memory.

To make a copy of the array, you should use the numpy.copy() method:

m = np.copy(n)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to copy array without changing original array?

From Dev

How to manipulate a JavaScript array passed to a function without changing the original argument array?

From Dev

How do I manipulate a copy of NSDictionary's contents without changing the contents of original

From Dev

How to modify/update an array in numpy without changing original

From Dev

Perl : How can I convert an array reference to an array, without creating a copy

From Dev

Perl : How can I convert an array reference to an array, without creating a copy

From Dev

C++ Copy Array Without dependency on original

From Dev

Create a duplicate of an array and manipulate the original

From Dev

take copy of an array without affecting original array in Angular 2

From Dev

Changing a value in a copy of an array changes the value in the original arra

From Dev

C# How to copy a file without changing the original file name

From Dev

How can I copy a Matlab array to a field of an array of structs

From Dev

Sort each row in 2D array without changing the original

From Dev

I can't copy the array

From Dev

How can I make an array without one number of the other array?

From Dev

How can I modify an array without destroying an array assigned to it?

From Dev

copying an array of objects and then modifying the original without affecting the copy

From Dev

How to manipulate array in section?

From Dev

Copying lists: editing copy without changing original

From Dev

Copying lists: editing copy without changing original

From Dev

How can I copy BitmapData into Byte array using C#?

From Dev

How do I use a shader without changing original color

From Dev

How can I make a new Array and copy all the postive elements from the other array in the new array and return it?

From Dev

How can I manipulate elements without recalculating the layout using jQuery

From Dev

How can I make changes to some Copied list that contains reference-type objects without changing the original one

From Dev

a function to copy an array that could effect the original array?

From Dev

How to clone json array without changing the value of existing json array

From Dev

How do I manipulate the data within this array? javascript

From Dev

How can I shift an array of objects while wrapping without iteration?

Related Related

  1. 1

    How to copy array without changing original array?

  2. 2

    How to manipulate a JavaScript array passed to a function without changing the original argument array?

  3. 3

    How do I manipulate a copy of NSDictionary's contents without changing the contents of original

  4. 4

    How to modify/update an array in numpy without changing original

  5. 5

    Perl : How can I convert an array reference to an array, without creating a copy

  6. 6

    Perl : How can I convert an array reference to an array, without creating a copy

  7. 7

    C++ Copy Array Without dependency on original

  8. 8

    Create a duplicate of an array and manipulate the original

  9. 9

    take copy of an array without affecting original array in Angular 2

  10. 10

    Changing a value in a copy of an array changes the value in the original arra

  11. 11

    C# How to copy a file without changing the original file name

  12. 12

    How can I copy a Matlab array to a field of an array of structs

  13. 13

    Sort each row in 2D array without changing the original

  14. 14

    I can't copy the array

  15. 15

    How can I make an array without one number of the other array?

  16. 16

    How can I modify an array without destroying an array assigned to it?

  17. 17

    copying an array of objects and then modifying the original without affecting the copy

  18. 18

    How to manipulate array in section?

  19. 19

    Copying lists: editing copy without changing original

  20. 20

    Copying lists: editing copy without changing original

  21. 21

    How can I copy BitmapData into Byte array using C#?

  22. 22

    How do I use a shader without changing original color

  23. 23

    How can I make a new Array and copy all the postive elements from the other array in the new array and return it?

  24. 24

    How can I manipulate elements without recalculating the layout using jQuery

  25. 25

    How can I make changes to some Copied list that contains reference-type objects without changing the original one

  26. 26

    a function to copy an array that could effect the original array?

  27. 27

    How to clone json array without changing the value of existing json array

  28. 28

    How do I manipulate the data within this array? javascript

  29. 29

    How can I shift an array of objects while wrapping without iteration?

HotTag

Archive