0

I asked a similar question here the other day, but I could not make myself clear. Then I ask once again.

I wanted to visualize a noise model on CCD sensors by Mathematica (Mine is ver.9). The following was what I executed:

noise := RandomVariate[PoissonDistribution[10], {20, 20}];
plotnoise = DiscretePlot3D[noise[[i, j]], {i, 1, 20}, {j, 1, 20}, ExtentSize -> Full]

I got an error message besides the expected plot: Part::pspec.

What does the error message mean?
How can I circumvent the error?

Furthermore, I'm thinking to use Manipulate to simulate the effect of stacking of a lot of noise frames and embed it on my web page by use of Wolfram CDF:

noisemean[n_Integer] := Total[Table[noise, {n}]]/n;
noisestack = Manipulate[DiscretePlot3D[noisemean[m][[i, j]], {i, 1, 20}, {j, 1, 20}, ExtentSize -> Full], {m, 1, 256, 1, Appearance -> "Labeled"}]

So, definitely I want to remove the error.

Taiki Bessho
  • 886
  • 5
  • 15
  • I cannot reproduce your error in MMA 10.4.0. This is my result when I run the first portion of your code: image. Can you check that the attributes of DiscretePlot3D in your version of Mathematica include a Hold attribute? I.e. report on the result of evaluating: Attributes[DiscretePlot3D]. – MarcoB Jul 05 '16 at 17:39
  • 1
    @MarcoB The result was {Protected, ReadProtected}. What does it mean? I'm a beginner...sorry – Taiki Bessho Jul 05 '16 at 17:45
  • @TaikiBessho - are you using version 9? I can reproduce the error in v9 but not v10 – Jason B. Jul 05 '16 at 17:50
  • @JasonB Yes, I'm using ver.9. Is there any idea to circumvent this error on ver.9? – Taiki Bessho Jul 05 '16 at 17:51
  • 3
    I have a workaround, but I wanted to check why you define your noise with a SetDelayed instead of Set? Every time you call for a value noise[[i,j]], it builds up 400 random numbers only to use one of them. – Jason B. Jul 05 '16 at 17:55
  • @JasonB Actually, for the first portion of my code, just set= is better. However, for the 2nd portion of my code, it has to sum up every different noise frames each time. For the purpose, I used SetDelayed:= instead of Set=. Maybe not smart way... – Taiki Bessho Jul 05 '16 at 18:03
  • 2
    @MarcoB - If I run Attributes[DiscretePlot3D] from a session where I haven't run DiscretePlot3D, then it returns the 2-element list that Taiki wrote above. But if I ask for the attributes after running a DiscretePlot3D example, then it gives me the HoldAll as well. I get this in version 9 and 10 – Jason B. Jul 05 '16 at 18:16
  • @JasonB I see. It would seem that the first execution triggers the loading of some code behind-the-scenes. That's interesting on its own, but maybe not relevant to Taiki's problem. Thanks for checking anyway. – MarcoB Jul 05 '16 at 18:19

1 Answers1

6

This is similar to the problem you get when you try to use an interpolating function inside a numeric integral, and the solution is similar: Define a helper function that only calls Part when the arguments are both integers:

noise := RandomVariate[PoissonDistribution[10], {20, 20}];
noisefunc[i_Integer, j_Integer] := noise[[i, j]];
DiscretePlot3D[noisefunc[i, j], {i, 1, 20}, {j, 1, 20}, 
 ExtentSize -> Full]

and it works without issue.

I do need to point out that, the way you've written this code, it generates 400 random numbers for every plotted data point. So you are generating $400^2$ random values and only plotting 400 of them. You could either define noise = RandomVariate[PoissonDistribution[10], {20, 20}] instead, or set it up like

DiscretePlot3D[
 RandomVariate[PoissonDistribution[10]], {i, 1, 20}, {j, 1, 20}, 
 ExtentSize -> Full]

and get the exact same plot.

For the Manipulate, I would do something like this:

Manipulate[
 DiscretePlot3D[
  Mean@
   RandomVariate[PoissonDistribution[10], m],
  {i, 1, 20}, {j, 1, 20}, ExtentSize -> Full], {m, 1, 256, 1, 
  Appearance -> "Labeled"}]

enter image description here

Jason B.
  • 68,381
  • 3
  • 139
  • 286