Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Scalars, vectors, matrices, and tensors, revisited for images

~14 min

Every technique in this course starts from one fact. A digital image is a block of numbers with a fixed shape. Every operation on the image is really an operation on that block of numbers. This section pins down four words before the first real image appears: scalar, vector, matrix, tensor. It uses them exactly the way linear-algebra defined them, then names which of the four shapes an image actually is.

Quick review

Practice from earlier coursework, pulled in for this topic.

From Linear Algebra — Vectors, Matrices, and the Geometry of Data

For A = \begin{bmatrix} 3 & -8 & 1 \\ 6 & 0 & -5 \end{bmatrix}, what is the entry in the top row, second column, a_{12}?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

a_{12} means row 1, column 2. Row 1 is (3, -8, 1), and its 2nd entry is -8.

Correct answer

Also accepted:

Why

From Linear Algebra — Vectors, Matrices, and the Geometry of Data

Which matrix is rectangular but neither square nor a row/column vector?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

This one has 2 rows and 2 columns — equal counts — which makes it square, not the plain rectangular case being asked for.

This has only 1 row (shape 1\times 3), which makes it a row vector, not a general rectangular matrix.

This has only 1 column (shape 3\times 1), which makes it a column vector, not a general rectangular matrix.

Why

\begin{bmatrix} 1 & 4 & 2 \\ 0 & -3 & 5 \end{bmatrix} has shape 2\times 3: more than one row, more than one column, and rows \neq columns — square and both vector shapes are ruled out, leaving 'rectangular.'

Rule out square matrices (rows = columns) and rule out row/column vectors (only 1 row or 1 column) — whatever's left is rectangular.

From Linear Algebra — Vectors, Matrices, and the Geometry of Data

A matrix D satisfies d_{ij} = 3i - 2j. Write an expression for d_{31} (numeric value, as an expression).

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Any equivalent form counts — 2x+3, 3 + 2·x, … Use the keypad below the field for fractions, roots, and other symbols.

Why

Substitute i=3, j=1 into the rule: d_{31} = 3(3) - 2(1) = 9 - 2 = 7.

Correct answer

Also accepted:

Why

Substitute i=3 and j=1 into the rule d_{ij}=3i-2j and simplify.

A scalar is one number, full stop. It could be the brightness of a single pixel, say 173 on a scale where 0 is black and 255 is white. Stack several numbers in a row and you get a vector. One pixel's red, green, and blue brightness together, (173, 40, 22), is a vector with three entries. Now arrange numbers in rows and columns, like a spreadsheet. That's a matrix. An entire black-and-white photo, one number per pixel, is nothing but a big matrix.

A tensor is what you get when "rows and columns" isn't enough bookkeeping. A color photo needs a row, a column, and a color label (red, green, or blue) to pin down one number. So a color photo is a tensor, with three locating numbers instead of a matrix's two.

Here's the useful way to picture a color photo. It's three ordinary black-and-white photos of the exact same size, glued together. One records only how much red sits at each spot. One records only green. One records only blue. Peel off just the "red" layer and you're holding an ordinary matrix again, the same kind of object as a grayscale image. Nothing new is happening mathematically. Color just means carrying three matrices instead of one.

A scalar is a single number. One pixel's brightness, 173, is a scalar. A vector is an ordered list of numbers, written \mathbf v \in \mathbb R^n. One row of a grayscale image is a vector, and so is one pixel's three color values (R, G, B). A matrix is a rectangular grid of numbers, M \in \mathbb R^{m\times n}. Two numbers locate each entry: a row and a column. A full grayscale image is a matrix. A tensor generalizes this idea to more than two locating numbers. A rank-2 tensor is just a matrix; a rank-3 tensor needs three numbers to name one entry.

A color image is a rank-3 tensor of shape (H, W, 3): height H, width W, and a third axis of length 3 for the red, green, and blue channels. Fix that third number to 0, 1, or 2 and let the other two vary, and the result is one ordinary matrix — the red channel, the green channel, or the blue channel. Each one is shaped exactly like a grayscale image. This is why almost every technique in this course gets built first for a single grayscale matrix, then extended to color afterward. A color image is really three (or more) matrices sharing one shape, processed together or one at a time.

A tiny grayscale image, as a matrix

This 3x4 block of numbers is a complete (if tiny) grayscale image -- three rows, four columns, one brightness value per entry, painted as a picture at the same time. Click the entry in row 2, column 3 and check its value against the matrix above.

Loading interactive…

Worked example

Reading a color pixel out of a tensor

  1. 1

    A color image img has shape (4, 5, 3): height 4, width 5, three color channels. What does img[2, 1, :] give you, and what shape does it have? The first two numbers, 2 and 1, fix one exact pixel: row 2, column 1. The : in the third slot means "every value along that axis," so every channel is included, none fixed.

  2. 2

    So img[2, 1, :] is the full color of that one pixel: a vector of length 3, (R, G, B), for instance (173, 40, 22), a fairly saturated red.

  3. 3

    Contrast this with img[:, :, 0], which fixes the last number to 0 (red) and lets both spatial numbers vary. That returns the entire red channel as its own 4\times 5 matrix, one number per pixel, no color mixing involved.

Loading interactive…

Worked example

Finding one entry in a bigger grayscale matrix

  1. 1

    Using the widget above, find the value stored at row 2, column 2 (0-indexed) of that 5\times 5 grayscale matrix. Row and column numbers both start counting at 0, top row and left column first.

  2. 2

    Clicking the cell at row 2, column 2 reads off 10, the center entry of the matrix. That matches the same 0-indexed row/column convention this section uses throughout.

Loading interactive…

Where this shows up

Astronomy and microscopy

A telescope or a microscope often doesn't capture "red, green, blue" at all. Instead it records a separate grayscale image through each of several narrow filters. Each filter passes only one band of light, not a color a human eye would know. The device then stacks those grayscale images into one tensor.

A famous Hubble Space Telescope picture, built from three filters named hydrogen-alpha, oxygen-III, and sulfur-II, has exactly the shape (H, W, 3) this section describes. It only gets shown on screen using red, green, and blue at the very end. That is just a choice of which filter to display through which color channel. All the way through the steps before that, the real object is a tensor of measured light values. It has no built-in notion of "color" at all.

Check your understanding (counts toward your progress)

Explain why img[10, 20] on a color image (only two indices supplied, not three) does not return a single brightness number.

A color image is naturally a rank-3 tensor rather than a matrix because...

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Pixel count doesn't determine tensor rank -- a huge grayscale image is still only rank 2.

The magnitude of the stored numbers has nothing to do with how many indices are needed to locate one.

A matrix could in principle hold any numbers -- the issue is that three indices are needed, not two, so a single matrix isn't the right shape.

Why

Fixing a pixel's color requires three coordinates -- row, column, and which channel (R, G, or B) -- so a color image needs three indices, the definition of rank 3.

For a color image img of shape (50, 80, 3), what does img[10, 20, :] return?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Both spatial indices are fixed at exact values, leaving only the color axis free -- that produces more than one number, not a lone scalar.

The row and column indices are both fixed to exact numbers here, not left free -- an 80\times 3 slice would come from leaving the column index free instead.

Both spatial indices ARE fixed here (to 10 and 20) -- a full matrix would only come back if a spatial index were left free with :.

Why

The first two indices are pinned to a single row and column; the trailing : leaves the channel axis free, returning that one pixel's three color values as a length-3 vector.

A grayscale image has 12 rows and 9 columns. How many total brightness values does it store?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

A matrix's entry count is rows times columns -- 12\times 9 = 108.

Correct answer

Also accepted:

Why

Practice: Naming and indexing shapes (4 problems · doesn’t affect your progress)

State the mathematical object (scalar, vector, matrix, or tensor) for each of the following, one at a time -- one pixel's brightness in a grayscale photo; one row of a grayscale photo; an entire grayscale photo; an entire color photo.

Count how many indices are needed to name one number in each case.

Given a color image of shape (100, 150, 3), name the shape of img[50, :, :] and describe what it represents.

For the same (100, 150, 3) image, name the shape of img[:, :, 1] and describe what it represents.

Explain why img[10, 20] on a color image (only two indices given, not three) does not return a single brightness number.

Feeling good about this section?

Take the section quiz

Every answer gets a full explanation at the end.

Graph settings

Calculator

Sign in to save highlights

Insert an equation

  • No matches.