# Plot 3D boolean Array or matrix in Mathematica

I have been attempting several methods to plot a 3D diagram of a n by n boolean array in Mathematica but none of them produced satisfactory result. I want the $3D$ plot to have a cube when M[i,j,k] == 1 and when M[i,j,k] == 0 just leaves as blank. Below is the best result I got.

L=10
M = RandomReal[{0, 1}, {L, L, L}];
For[k = 1, k <= L, k++,
For[j = 1, j <= L, j++,
For[i = 1, i <= L, i++,
If[M[[i, j, k]] < 0.5,
M[[i, j, k]] = 1,
M[[i, j, k]] = 0
]]]];
Image3D[M, ImageSize -> 500, Boxed -> True]


The Image3D function gives a very fluffy and opaque look on the cubes which I couldn't change!!

Is there someway I can change the opacity and color of the Image3D function in this case? Or is there a better way to plot this matrix?

-
Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the FAQs! 3) When you see good Q&A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign – Vitaliy Kaurov Apr 9 '14 at 6:57

## 1 Answer

L = 10;


First of all you could get Booolian array simply as (thx. to @Kuba)

RandomInteger[1,{L,L,L}]


But if you are interested in thresholding:

M = RandomReal[{0, 1}, {L, L, L}];


First of all do not binarize procedurally, use functional style, say:

bM = M /. x_ -> UnitStep[x - .5];


or image processing:

bM = ImageData[Binarize[Image3D[bM], .5]];


Then if you don't like Image3D - which I think is totally fine, use Graphics3D:

Table[Graphics3D[{Hue[RandomReal[]], {Opacity[k], Cuboid[#]} & /@
Position[bM, 1]}], {k, .01, 1, .19}]


Manipulate[
Graphics3D[Sphere[#, r] & /@ Position[bM, 1],
ViewVertical -> {-1, 0, 0}, Boxed -> False, Lighting -> "Neutral",
Background -> Black, SphericalRegion -> True]
, {r, .03, 1}]


-
@RahulNarain yes thank you – Vitaliy Kaurov Apr 9 '14 at 6:10
@Kuba sure I added it. I was just thinking he is interested in thresholding. – Vitaliy Kaurov Apr 9 '14 at 6:12
You are probably right :) – Kuba Apr 9 '14 at 6:13
Very useful stuff!! Thanks a lot guys! – user3513537 Apr 12 '14 at 5:33