1

np in this context is numpy.

z_mesh is array of shape 41*401.

I need dz_theta_mat and dz_gamma_mat of shape 20*200 by subtracting [2*i,2*j]th element from [2*i,2*j+2]th element and [2*i,2*j]th element from [2*i+2,2*j]th element respectively.

My attempt doesn't give the required shape for the list.

how do i modify it to obtain the required result?

print np.shape(z_mesh)
dz_gamma_mat = [z_mesh[2*i,2*j+2]-z_mesh[2*i,2*j] for i,j in itertools.product(range(20),range(199))]
print np.shape(fish_dz_gamma_mat)
dz_theta_mat = [z_mesh[2*i+2,2*j]-z_mesh[2*i,2*j] for i, j in itertools.product(range(19),range(200))]
print np.shape(fish_dz_theta_mat)

(41, 401)
(3980,)
(3800,)
bananagator
  • 551
  • 3
  • 8
  • 26

2 Answers2

5

Why not just slice and subtract?

dz_theta_mat = z_mesh[:-1:2, 2::2] - z_mesh[:-1:2, :-1:2]
dz_gamma_mat = z_mesh[2::2, :-1:2] - z_mesh[:-1:2, :-1:2]
Shai
  • 111,146
  • 38
  • 238
  • 371
0

You should cast your result as numpy array and then reshape your result, e.g.:

dz_theta_mat = np.array([z_mesh[2*i+2,2*j]-z_mesh[2*i,2*j] for i, j in itertools.product(range(19),range(200))]).reshape(z_mesh.shape)
norok2
  • 25,683
  • 4
  • 73
  • 99