Subtract 1 from the first item in the Q2 array, 2 from the second item, 3 from the third item..n for the nth item and call it Q3
Question:
i’m working on a school assignment. I have an array called Q2 and it wants me to create Q3 by subtracting . The actual array is much longer but this is the first few values:
Q2 = [341, 50178, 356)
my advised creating a second array that can be used in combination with the first and that we may want to use the linspace() method. She also suggested not using a for loop
the output should be:
array([340, 50176, 353)
I’m really stuck and not sure how to proceed. I read about the linspace method but i don’t understand how that could result in a decrease in values. I tried using the subtract method but it says it’s in the wrong shape. Does anyone have any guidance?
Answers:
To create the array Q3 by subtracting a certain value from each element of Q2, you can use numpy library which provides various mathematical operations on arrays. Here’s one way to do it.
You can simply subtract every index value from that particular entry and won’t even require a new array. Here is the code
import numpy as np
#Let’s say you have a sample array
arr = np.array(1,2,3)
for x in arr:
arr[x]-=x
You can use np.linspace
to generate an array to subtract from Q2
, by passing it parameters:
- start: 1
- stop: Q2.size
- num: Q2.size
Then you can assign Q3
from the subtraction of that from Q2
:
Q2 = np.array([341, 50178, 356])
sub = np.linspace(1, Q2.size, Q2.size)
# array([1., 2., 3.])
Q3 = Q2 - sub
# array([ 340., 50176., 353.])
Note that for this particular use case, using np.arange
(as described in @mozway answer) instead of linspace
would be more efficient.
i’m working on a school assignment. I have an array called Q2 and it wants me to create Q3 by subtracting . The actual array is much longer but this is the first few values:
Q2 = [341, 50178, 356)
my advised creating a second array that can be used in combination with the first and that we may want to use the linspace() method. She also suggested not using a for loop
the output should be:
array([340, 50176, 353)
I’m really stuck and not sure how to proceed. I read about the linspace method but i don’t understand how that could result in a decrease in values. I tried using the subtract method but it says it’s in the wrong shape. Does anyone have any guidance?
To create the array Q3 by subtracting a certain value from each element of Q2, you can use numpy library which provides various mathematical operations on arrays. Here’s one way to do it.
You can simply subtract every index value from that particular entry and won’t even require a new array. Here is the code
import numpy as np
#Let’s say you have a sample array
arr = np.array(1,2,3)
for x in arr:
arr[x]-=x
You can use np.linspace
to generate an array to subtract from Q2
, by passing it parameters:
- start: 1
- stop: Q2.size
- num: Q2.size
Then you can assign Q3
from the subtraction of that from Q2
:
Q2 = np.array([341, 50178, 356])
sub = np.linspace(1, Q2.size, Q2.size)
# array([1., 2., 3.])
Q3 = Q2 - sub
# array([ 340., 50176., 353.])
Note that for this particular use case, using np.arange
(as described in @mozway answer) instead of linspace
would be more efficient.