We’ve seen how Table can be used to make lists. Now let’s see how Table can be used to create higher-dimensional arrays of values.

Table[x,4]

Table[x,4,5]

Grid[Table[x,4,5]]

You can use Table with two variables to make a 2D array. The first variable corresponds to the row; the second to the column.

Grid[Table[RGBColor[r,0,b],{r,0,1,.2},{b,0,1,.2}]]

Grid[Table[i,{i,4},{j,5}]]

Grid[Table[j,{i,4},{j,5}]]

Grid[Table[i+j,{i,5},{j,5}]]

Grid[Table[i*j,{i,5},{j,5}]]

ArrayPlot lets you visualize values in an array. Larger values are shown darker.

ArrayPlot[Table[i*j,{i,5},{j,5}]]

ArrayPlot[Table[RandomInteger[10],30,30]]

ArrayPlot[Table[RandomColor[ ],30,30]]

Images are ultimately arrays of pixels. Color images make each pixel have red, green and blue values. Black-and-white images have pixels with values 0 (black) or 1 (white). You can get the actual pixel values using ImageData.

ImageData[Binarize[ Rasterize["W"] ]]

ArrayPlot[ImageData[Binarize[ Rasterize["W"] ]]]

The image is of very low resolution, because that’s how Rasterize made it in this case. It’s also white-on-black instead of black-on-white. That’s because in an image 0 is black and 1 is white (like in RGBColor), while ArrayPlot’s default is to make larger values darker.

You can do arithmetic with arrays, just like lists. That means it’s easy to swap 0 and 1 in this array: Just subtract everything from 1, so every 0 becomes 1−0=1, and every 1 becomes 1−1=0.

1-ImageData[Binarize[ Rasterize["W"] ]]

ArrayPlot[1-ImageData[Binarize[ Rasterize["W"] ]]]

Make a 12×12 multiplication table. »

Make a 5×5 multiplication table for Roman numerals. »

Make a 10×10 grid of random colors. »

Make a 10×10 grid of randomly colored random integers between 0 and 10. »

Make a grid of all possible strings consisting of pairs of letters of the alphabet (“aa”, “ab”, etc.). »

Visualize {1,4,3,5,2} with a pie chart, number line, line plot and bar chart. Place these in a 2×2 grid. »

Make an array plot of hue values x*y, where x and y each run from 0 to 1 in steps of 0.05. »

Make an array plot of hue values x/y, where x and y each run from 1 to 50 in steps of 1. »

Make an array plot of the lengths of Roman numeral strings in a multiplication table up to 100×100. »

Make a 20×20 addition table. »

Make a 10×10 grid of randomly colored random integers between 0 and 10 that have random size up to 32. »

Can the limits of one variable in a table depend on another?

Yes, later ones can depend on earlier ones. Table[x,{i,4},{j,i}] makes a “ragged” triangular array.

Can I make tables that are lists of lists of lists?

Yes, you can make tables of any dimension. Image3D gives a way to visualize 3D arrays.

Why does 0 correspond to black, and 1 to white, in images?

0 means zero intensity of light, i.e. black. 1 means maximum intensity, i.e. white.

How do I get the original image back from the output of ImageData?

Just apply the function Image to it.

Tech Notes