Skip to content

Commit 7ed0c61

Browse files
authored
Merge pull request #255 from Nin17/master
correct 2D Gaussian + add separable alternative answer
2 parents e865a95 + fa2dadf commit 7ed0c61

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

source/exercises100.ktx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,18 @@ Generate a generic 2D Gaussian-like array (★★☆)
719719
hint: np.meshgrid, np.exp
720720

721721
< a56
722-
X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))
723-
D = np.sqrt(X*X+Y*Y)
722+
X, Y = np.meshgrid(np.linspace(-1, 1, 10), np.linspace(-1, 1, 10))
724723
sigma, mu = 1.0, 0.0
725-
G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )
724+
G = np.exp(-((X - mu) ** 2 + (Y - mu) ** 2) / (2.0 * sigma**2))
725+
print(G)
726+
727+
# Another solution using the fact that a 2D Gaussian is separable
728+
# Author: Nin17
729+
x = np.linspace(-1, 1, 10)
730+
y = np.linspace(-1, 1, 10)
731+
gx = np.exp(-((x - mu) ** 2 / (2.0 * sigma**2)))
732+
gy = np.exp(-((y - mu) ** 2 / (2.0 * sigma**2)))
733+
G = gy[:, None] * gx[None, :]
726734
print(G)
727735

728736
< q57

0 commit comments

Comments
 (0)