I have implemented a simple version of the kernel-based convolution operation as shown below. However, for faster code operation, I think the nested for loop is slowing up the operation. Is there any quicker implementation of the code possible without the nested loop? def kernel_convolution(matrix,kernel): if not isinstance(matrix, np.ndarray): matrix = np.array(matrix) n, m = matrix.shape if not isinstance(kernel, np.ndarray): kernel = np.array(kernel) k = kernel.shape matrix_to_kernel_is_correct = n >= k[0] and m >= k[1] assert matrix_to_kernel_is_correct, 'Kernel can\'t be bigger than matrix in terms of shape.' stride=(1,1) h_out = np.floor((n - (k[0] - 1) -1) / stride[0] + 1).astype(int) w_out = np.floor((m - (k[1] - 1) -1) / stride[1] + 1).astype(int) out_dimensions_are_correct = h_out > 0 and w_out > 0 assert out_dimensions_are_correct, 'Can\'t apply input parameters, one of resulting output dimension is non-positive.' matrix_out = np.zeros((h_out, w_out)) b = k[0] // 2, k[1] // 2 center_x_0 = b[0] center_y_0 = b[1] start=(0,0) for i in range(h_out): curr_start_x = start[0] + i * stride[0] if curr_start_x<=n-k[0]: for j in range(w_out): curr_start_y = start[1] + j * stride[1] if curr_start_y<=m-k[1]: submatrix = matrix[curr_start_x:curr_start_x+k[0], curr_start_y:curr_start_y+k[1]] matrix_out[j] = np.sqrt(np.sum((submatrix-kernel)**2)) return matrix_out Continue reading...