# Part::pspec error occured

I wanted to simulate a noise model on CCD sensors, so I did as below:

noise[i_, j_] := RandomVariate[PoissonDistribution[50], {20, 20}][[i, j]];
noiseimage = DiscretePlot3D[noise[i, j], {i, 1, 20, 1}, {j, 1, 20, 1}, ExtentSize -> Full]


Then I got an error message "Part::pspec" but the desired image appeared.
What was wrong with my code? Thank you in advance.

• Works without error for me on V10.4.1. Have you tried on a fresh kernel to avoid problems with previous assignments? – Yves Klett Jun 17 '16 at 15:48
• @YvesKlett I was working on Mathematica 9.0. I've refreshed the kernel before executing the above commands. – Taiki Bessho Jun 17 '16 at 16:05

You are simulating new values everytime you call noise[_,_]. To simulate once and plot that you could rewrite it as:

(noise[i_, j_] = #[[i, j]]) &[RandomVariate[PoissonDistribution[50], {20, 20}]];
noiseimage = DiscretePlot3D[noise[i, j], {i, 1, 20, 1}, {j, 1, 20, 1}, ExtentSize -> Full]


The error message concerning the symbolic part argument is not really an error, just a warning.

• I understood your code but arose a question that why you used the pure function #. I thought just eliminating the colon : before the equal sign = in the definition of noise[_,_] is easier and sufficient, i.e. noise[i_, j_] = RandomVariate[PoissonDistribution[50], {20, 20}][[i, j]]; Did I miss something important concepts to write Mathematica codes? – Taiki Bessho Jun 17 '16 at 22:41
• @TaikiBessho You are right, the pure function is only needed when using :=. – Coolwater Jun 18 '16 at 8:44
• I tried your command above in my Mathematica(ver.9) but I got the error message again... Then, I wonder how should I do for not getting the error message... – Taiki Bessho Jul 5 '16 at 11:23
• Or I don't have to mind the error? – Taiki Bessho Jul 5 '16 at 12:33

Simpler and faster is:

ArrayPlot[RandomVariate[PoissonDistribution[50], {20, 20}]]

• Yeah, it might be useful for some people. I, however, want to get the noise levels not in grayscale but in heights of rectangular blocks. Anyway, thank you. – Taiki Bessho Jun 17 '16 at 16:10