nX=20; % number of grid pts along x, y and z
nY=20;
nZ=20;

mapFid = fopen('map.dat', 'w+'); % file in which material map will be written

xc = nX/2; % center of pyramid basis
yc = nY/2;
zc = nZ/4;

a=10; % pyramid base length

for x = 1:nX % loop over all three dimensions
   for y = 1:nY
      for z = 1:nZ
      aZ = a - (z-zc);
      if (
            (x>=xc-aZ/2) && (x<=xc+aZ/2)
         && (y>=yc-aZ/2) && (y<=yc+aZ/2)
         && (z >= zc) && (z <= zc+a))
      m1 = 1;
      else m1 = 0;
      endif
      m2 = 1 - m1; % ensure that m1 + m2 = 1 for all x,y,z
  
      fprintf(mapFid, '%f   %f\n', m1, m2); % write map file

      endfor
   endfor
endfor

fclose(mapFid);

