Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

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]

enter image description here

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?

share|improve this question
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
L = 10;

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


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}]

enter image description here

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

enter image description here

share|improve this answer
@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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.