# padding with zero in Table

I am creating a grid as

Print["no of grid points, lattice length, dz " , {max = 10, L = 10.,dz=L/max}]
grid = Table[-L/2 + (n - 1) dz, {n, 1, max}]
grid2 = Table[-L/2 + (n - 1) dz, {n, 1, max, 2}]

The output is During evaluation of

no of grid points, co-ordinate lattice length, dz {10,10.,1.}

{-5., -4., -3., -2., -1., 0., 1., 2., 3., 4.}

{-5., -3., -1., 1., 3.}

I want to pad zeros in the grid2 on the places where I have not shown element. Means I need

{-5., 0., -3., 0., -1., 0., 1., 0., 3., 0.}

How can I do that?

Also what if I want to pad two or more zeroes as

{-5., 0., 0., -2., 0, 0., 1., 0., 0., 4.}

max = 10; L = 10.; dz = 1;

grid2 = Riffle[Table[-L/2 + (n - 1) dz, {n, 1, max, 2}], ConstantArray[0., max/2]]
(* or  Riffle[Table[-L/2 + (n - 1) dz, {n, 1, max, 2}], 0., {2, -1, 2}] *)
(* {-5., 0., -3., 0., -1., 0., 1., 0., 3., 0.} *)

or

grid2b =Join @@ Table[{-L/2 + (n - 1) dz, 0.}, {n, 1, max, 2}]
(* {-5., 0., -3., 0., -1., 0., 1., 0., 3., 0.} *)

or

grid2c = Join @@ Thread[{Table[-L/2 + (n - 1) dz, {n, 1, max, 2}], 0.}]
(* {-5., 0., -3., 0., -1., 0., 1., 0., 3., 0.} *)

Update:

f0 = Module[{ca = ConstantArray[0., Length@#],
indices = Range[1, max, #2 + 1]}, ca[[indices]] = #[[indices]]; ca] &;

grid = Table[-L/2 + (n - 1) dz, {n, 1, max}];
f0[grid, 1]
(*  {-5., 0., -3., 0., -1., 0., 1., 0., 3., 0.} *)

f0[grid, 2]
(* {-5., 0., 0., -2., 0., 0., 1., 0., 0., 4.}  *)

Using ReplacePart:

f1 = ReplacePart[#, Except[Alternatives @@ Range[1, max, #2 + 1]] -> 0.] &;

grid = Table[-L/2 + (n - 1) dz, {n, 1, max}];
f1[grid, 1]
(* {-5.,0.,-3.,0.,-1.,0.,1.,0.,3.,0.} *)
f1[grid, 2]
(* {-5.,0.,0.,-2.,0.,0.,1.,0.,0.,4.} *)

Using MapAt:

f2 = MapAt[0. &, #, {{Complement[Range[max], Range[1, max, #2 + 1]]}}] &;

grid = Table[-L/2 + (n - 1) dz, {n, 1, max}];
f2[grid, 1]
(* {-5.,0.,-3.,0.,-1.,0.,1.,0.,3.,0.} *)
f2[grid, 2]
(* {-5.,0.,0.,-2.,0.,0.,1.,0.,0.,4.} *)
• The edited version is another aspect of such kind of problems, helping to learn something new. However, my question was to replace {-5., -4., -3., -2., -1., 0., 1., 2., 3., 4.} with this {-5., 0., 0., -2., 0, 0., 1., 0., 0., 4.} Nov 10, 2014 at 17:58
• @zenith, pls see the last update..
– kglr
Nov 10, 2014 at 18:09
• Thank you very much for suggestions. The one using RelacePart I tried. The one using MapAt is generating this message "Position specification {{2,3,5,6,8,9}} in \ MapAt[0.&,{-5,-4,-3,-2,-1,0,1,2,3,4},{{{2,3,5,6,8,9}}}] is not an \ integer or a list of integers. " Nov 11, 2014 at 10:18
• @zenith, all three functions work as expected in v9.0.1.0 (windows 8) and on v10 (free wolfram programming cloud version). What you are getting may be a version/os issue.
– kglr
Nov 11, 2014 at 10:25
• Alright, thank you so much. I use the one which is working correct on my machine. Nov 11, 2014 at 10:33

Here is a very simple solution to "pad" your grid :

If :

grid={-5., -4., -3., -2., -1., 0., 1., 2., 3., 4.}

then you can just multiply it this way :

grid*{1, 0, 1, 0, 1, 0, 1, 0, 1, 0}
(*{-5., 0., -3., 0., -1., 0., 1., 0., 3., 0.}*)

or

grid*{1, 0, 0, 1, 0, 0, 1, 0, 0, 1}
(*{-5., 0., 0., -2., 0., 0., 1., 0., 0., 4.} *)

To generate automatically the mask list {1,0,..}, you can use this function :

periodicMask[int_Integer, maxpoints_] := Table[Floor[Mod[i - 2, int]/(int - 1)], {i, maxpoints}]

For example :