Home > Software engineering >  Numpy: How can I create a linspace in 3D dimension?
Numpy: How can I create a linspace in 3D dimension?

Time:10-20

as a homework task I was given to create an RGB spectrum image just with numpy functions.

This is my current code:

zero = np.dstack([
    np.linspace(0.0, 1.0, self.resolution),
    np.linspace(0.0, 0.0, self.resolution),
    np.linspace(1.0, 0.0, self.resolution)
])
spectrum = np.tile(zero, (self.resolution, 1, 1))

What this produces is a gradient from red to blue. Now, what is left is to linspace the green value into the third dimension.

Anyone here who has some tips how to do that?

Edit: Let me re-phrase - how can I avoid this loop with numpy?

spectrum = np.tile(zero, (self.resolution, 1, 1))
for i in range(self.resolution):
    spectrum[i, :, 1] = green[i]

CodePudding user response:

Your last for loop is:

spectrum[:, :, 1] = np.linspace(0.0, 1.0, resolution)[:, None]
  • Related