Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refine a mesh in python quickly

Tags:

python

numpy

I have a numpy array([1.0, 2.0, 3.0]), which is actually a mesh in 1 dimension in my problem. What I want to do is to refine the mesh to get this: array([0.8, 0.9, 1, 1.1, 1.2, 1.8, 1.9, 2, 2.1, 2.2, 2.8, 2.9, 3, 3.1, 3.2,]).

The actual array is very large and this procedure costs a lot of time. How to do this quickly (maybe vectorize) in python?

like image 597
atbug Avatar asked Aug 05 '26 04:08

atbug


1 Answers

Here's a vectorized approach -

(a[:,None] + np.arange(-0.2,0.3,0.1)).ravel() # a is input array

Sample run -

In [15]: a = np.array([1.0, 2.0, 3.0])  # Input array

In [16]: (a[:,None] + np.arange(-0.2,0.3,0.1)).ravel()
Out[16]: 
array([ 0.8,  0.9,  1. ,  1.1,  1.2,  1.8,  1.9,  2. ,  2.1,  2.2,  2.8,
        2.9,  3. ,  3.1,  3.2])
like image 161
Divakar Avatar answered Aug 06 '26 19:08

Divakar