0

I want to create a vertical array according to the following

import numpy as np

L = 2**15
dx = 0.1
x = np.arange(-L/2,L/2)*dx
x=x.reshape((L,1)) 

This creates an array of dimension L,1 which is fine but is there a cleaner way to do this? I feel my solution is clunky

Dimitri_896
  • 137
  • 4
  • Where is `N` defined? – Dani Mesejo Oct 17 '21 at 20:29
  • 1
    Sorry, N should be L. edited – Dimitri_896 Oct 17 '21 at 20:32
  • What's wrong with reshape? You can also chain the commands: `x = np.arange(-L/2,L/2).reshape(-1,1) * dx`. – Quang Hoang Oct 17 '21 at 20:47
  • Reposting a question just because you aren't satisfied with the answers you got is not considered good SO behavior. Comment on the answer you were given if necessary. If you must build on something earlier, at least acknowledge the previous answers. – hpaulj Oct 17 '21 at 21:07
  • It's not an explicit repost. I'm asking how to streamline something I did previously. I didn't realise this contradicts SO policies and won't do it again. – Dimitri_896 Oct 17 '21 at 22:28

1 Answers1

0

You could use:

x = (np.arange(-L/2,L/2)*dx)[:, None]

Or:

X = L*dx/2
np.arange(-X,X,dx)[:, None]

Though you might accumulate errors with the second approach due to floating point calculations.

mozway
  • 194,879
  • 13
  • 39
  • 75