Tensor
Iteration
Multidimensional arrays
Data manipulation
Programming tutorials

How to iterate over the rows, columns and planes of a tensor?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When people say "rows, columns, and planes" of a tensor, they are usually talking about axes of a multidimensional array. The clean way to iterate is to define what each axis means first and then either slice directly or move axes into the position you want to loop over.

Define the Shape Convention

Suppose a 3D tensor has shape (planes, rows, cols). Then:

  • axis 0 indexes planes
  • axis 1 indexes rows within each plane
  • axis 2 indexes columns within each row

Using a fixed axis convention prevents most confusion.

Example Tensor

python
1import numpy as np
2
3t = np.arange(24).reshape(2, 3, 4)
4print(t.shape)

This creates:

  • '2 planes'
  • '3 rows per plane'
  • '4 columns per row'

Iterating Over Planes

If planes live on axis 0, iterate directly:

python
for plane_index, plane in enumerate(t):
    print("plane", plane_index)
    print(plane)

Each plane is a 2D array of shape (3, 4).

Iterating Over Rows

Rows are easiest to interpret within each plane:

python
for plane_index, plane in enumerate(t):
    for row_index, row in enumerate(plane):
        print("plane", plane_index, "row", row_index, row)

If you only want rows from one plane, slice the plane first:

python
for row in t[0]:
    print(row)

Iterating Over Columns

Columns are not stored in the same convenient leading position as rows, so transpose or move the axis first.

For columns within one plane:

python
for col_index, col in enumerate(t[0].T):
    print("column", col_index, col)

Because t[0] has shape (rows, cols), its transpose exposes the columns as iterable rows.

Using moveaxis for General Axis Iteration

When you want a uniform approach, np.moveaxis is very helpful:

python
1columns_view = np.moveaxis(t, 2, 0)
2
3for col_index, col_block in enumerate(columns_view):
4    print("column axis slice", col_index)
5    print(col_block)

This moves the column axis to the front so you can iterate over it directly.

The same idea works for any axis, not just columns.

Why Slicing Is Better Than Scalar Loops

In numerical code, you usually want to iterate over meaningful slices, not scalar elements. Scalar-by-scalar loops are slower and often harder to read.

Good tensor iteration usually looks like:

  • plane by plane
  • row by row
  • batch by batch
  • channel by channel

That keeps the code aligned with the structure of the data.

TensorFlow Note

If you are working with TensorFlow tensors instead of NumPy arrays, eager-mode Python loops over the leading axis can work for small tasks, but vectorized tensor operations are usually better.

Many problems that seem to require manual iteration can instead be handled with slicing, transposition, reduction, or broadcasting.

Common Pitfalls

The biggest mistake is not defining what each axis represents before iterating. Without that, "row" and "plane" become ambiguous.

Another mistake is trying to iterate over columns exactly the same way as rows without rearranging axes.

People also drop into scalar-by-scalar loops too early. In tensor code, slice-level operations are usually faster and clearer.

Finally, if the tensor shape changes during preprocessing, re-check the axis order before reusing old iteration code.

Summary

  • Decide first what each tensor axis represents.
  • For a shape (planes, rows, cols), planes are axis 0, rows are axis 1, and columns are axis 2.
  • Iterate over planes and rows directly with normal slicing.
  • Use transpose or moveaxis when you want to loop over a non-leading axis such as columns.
  • Prefer slice-level iteration over scalar-by-scalar loops whenever possible.

Course illustration
Course illustration

All Rights Reserved.