Reputation: 113
I'm trying to generate sinusiod image but i see no alternative without using two for loops. Can someone help me vectorize this code?
import numpy as np
M, N = 1001, 1001
u = 100
v = 200
sin_img = np.zeros((M, N))
for m in range(M):
for n in range(N):
sin_img[m, n] = np.sin((2 * np.pi * u * m / M) + (2 * np.pi * v * n / N))
Upvotes: 2
Views: 94
Reputation: 19322
Here is the proper vectorized
way of doing it without any loops what so ever.
5,5
, then this matrix should hold tuples of indexes for each of its positions and therefore be a 5,5,2
matrix. So 0th row, 0th column should hold (0,0)
, 4th row and 2nd column should hold (4,2)
and so on.1001,1001,2
created using np.indices
, you can use that in a vectorized way in the numpy functions with 1001, 1001, 1
in each of the 2 parts.np.sin
on the sum of the 2 parts.idx_sin_img = np.stack(np.indices((M,N)), axis=-1) #(1001,1001,2)
out = np.sin((2 * np.pi * u * idx_sin_img[...,0] / M) +
(2 * np.pi * v * idx_sin_img[...,1] / N))
out.shape
(1001, 1001)
Just to confirm that it gives the same output -
np.all(sin_img == out) #sin_img is from OP's own code
#True
Benchmarks -
Upvotes: 2
Reputation: 1570
I don't know what "vectorize" means but if you want to get rid of the two for loops you can use a list comprehension:
sin_img = np.array([[np.sin((2 * np.pi * u * m / M) + (2 * np.pi * v * n / N))
for n in range(N)]
for m in range(M)])
Explanation:
The code is made up from a list comprehension inside a list comprehension. Each list comprehension is made like this:
[some_expression for NAME in some_iterable]
where the some_expression
part can be another list comprehension.
Upvotes: 2