Generate a $4 \times 4\times 16$ binary matrix using RandomInteger
and replace 0
s with Red
and 1
s with Green
:
SeedRandom[1]
matrix = RandomInteger[1, {4, 4, 16}] /. {0 -> Red, 1 -> Green};
Use MapIndexed
with level specification {3}
to combine colors in matrix
with spheres and cuboids placed in coordinates given by the index:
spheres = MapIndexed[{#, Sphere[#2, .15]} &, matrix, {3}];
cuboids = MapIndexed[Cuboid @ #2 &, matrix[[;; -2, ;; -2, ;; -2]], {3}];
Graphics3D[{spheres, FaceForm[{Opacity[.1], Blue}], cuboids}, Boxed -> False]

Alternatively, generate coordinates using Outer
and combine the coordinates with the colors from matrix
using MapThread
:
coords = Outer[List, Range[4], Range[4], Range[16]];
spheres = MapThread[{#, Sphere[#2, .15]} &, {matrix, coords}, 3];
cuboids = Map[Cuboid, coords[[;;-2, ;;-2, ;;-2]], {-2}];
Graphics3D[{spheres, FaceForm[{Opacity[.1], Blue}], cuboids}, Boxed -> False]

Note: You can use RandomChoice
to get the 3D array of colors directly:
SeedRandom[1];
matrix2 = RandomChoice[{Red, Green}, {4, 4, 16}];
matrix2 == matrix
True