1

I have an array populated with the same value, however, I want this value to incrementally decrease by a value of 0.025 with each row. It currently looks like this:

import numpy as np

vp_ref = 30
surf_lay = np.ones(([1000,10000]), dtype=np.float32);
gvp_ref = vp_ref * surf_lay

So the array is filled with 30s. I want the first row to be 30, decrease to 29.975 in the next row, and keep going until the bottom. How would I do this?

2
  • 1
    Can you explain why you need such an array? You might be facing an XY problem. Commented Apr 30, 2020 at 11:55
  • Better to say 'numpy' not Python, since you're using np.array. (Python has arrays too, but they're completely different and they suck). Commented Apr 30, 2020 at 13:38

3 Answers 3

2

Here's a solution:

  1. Define step_range to get all the values starting from 0, adding step until the end of your matrix size.
  2. Subtract it!
step = 0.025
step_range = np.arange(0, gvp_ref.shape[0] * step, step).reshape(-1, 1)
print(gvp_ref - step_range)

Output:

array([[30.   , 30.   , 30.   , ..., 30.   , 30.   , 30.   ],
       [29.975, 29.975, 29.975, ..., 29.975, 29.975, 29.975],
       [29.95 , 29.95 , 29.95 , ..., 29.95 , 29.95 , 29.95 ],
       ...,
       [ 5.075,  5.075,  5.075, ...,  5.075,  5.075,  5.075],
       [ 5.05 ,  5.05 ,  5.05 , ...,  5.05 ,  5.05 ,  5.05 ],
       [ 5.025,  5.025,  5.025, ...,  5.025,  5.025,  5.025]])
Sign up to request clarification or add additional context in comments.

2 Comments

Calling np.full broadcasting is a bit of a stretch. You could use np.broadcast_to and then it would be true. Actually, I'm pretty sure broadcasting would work as gvp_ref - step_range...
I thought gvp_ref - step_range would work but it didn't initially. In fact, with .reshape(1000,1), you're right, it does! Thanks @AndrasDeak, edited, that's cleaner.
1

You can use np.linspace for creating the linearly spaced data and then np.tile for creating the 2D array:

n = 1000
tmp = np.linspace(30, 30 - (n-1)*0.025, n)
result = np.tile(tmp[:, None], (1, 10_000))

Comments

1

Here, in this code alpha is 0.025

import numpy as np
vp_ref = 30
surf_lay = np.ones(([1000,10000]), dtype=np.float32);
gvp_ref = vp_ref * surf_lay

alpha = 0.025
substration_array = np.array([[alpha*i]*gvp_ref.shape[1] for i in range(gvp_ref.shape[0])])

gvp_ref.shape
substration_array.shape

output = np.subtract(gvp_ref, substration_array)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.