TheAlgorithms/Go · error
rows have different numbers of columns
Error message
rows have different numbers of columns
What it means
NewFromElements rejects the input when IsValid(elements) fails, i.e. the rows of the 2-D slice do not all have the same number of columns. A matrix must be rectangular, so a jagged slice is invalid input.
Source
Thrown at math/matrix/matrix.go:45
for i := range elements {
go func(i int) {
defer wg.Done()
elements[i] = make([]T, columns)
for j := range elements[i] {
elements[i][j] = initial
}
}(i)
}
wg.Wait()
return Matrix[T]{elements, rows, columns}
}
// NewFromElements creates a new Matrix from the given elements.
func NewFromElements[T constraints.Integer](elements [][]T) (Matrix[T], error) {
if !IsValid(elements) {
return Matrix[T]{}, errors.New("rows have different numbers of columns")
}
rows := len(elements)
if rows == 0 {
return Matrix[T]{}, nil // Empty matrix
}
columns := len(elements[0])
matrix := Matrix[T]{
elements: make([][]T, rows),
rows: rows, // Set the rows field
columns: columns, // Set the columns field
}
for i := range matrix.elements {
matrix.elements[i] = make([]T, columns)
copy(matrix.elements[i], elements[i])
}
return matrix, nilView on GitHub (pinned to 5ba447ec5f)
Solutions
- Validate the slice before construction: check len(row) is identical for every row.
- Fix the offending row in the input data so all rows have equal length.
- If ragged input is expected, pad missing cells with a zero value before calling NewFromElements.
Example fix
// before
m, err := math.NewFromElements([][]int{{1, 2}, {3}}) // error: jagged
// after
m, err := math.NewFromElements([][]int{{1, 2}, {3, 0}}) Defensive patterns
Strategy: validation
Validate before calling
func validElements[T constraints.Integer](els [][]T) bool {
if len(els) == 0 {
return true
}
n := len(els[0])
for _, row := range els {
if len(row) != n {
return false
}
}
return true
}
if !validElements(raw) {
return errors.New("input rows must all have the same length")
}
m, err := math.NewFromElements(raw) Type guard
func isRectangular[T constraints.Integer](els [][]T) bool {
for _, row := range els {
if len(row) != len(els[0]) {
return false
}
}
return true
} Try / catch
m, err := math.NewFromElements(raw)
if err != nil {
// raw is jagged: find and fix the offending row
return fmt.Errorf("invalid matrix input: %w", err)
} Prevention
- Validate external data (CSV/JSON rows) for equal lengths before building matrices.
- Write a test that feeds a jagged slice and expects the error.
- Pad missing fields at parse time instead of letting the constructor fail.
When it happens
Trigger: Calling NewFromElements with [][]T{{1,2},{3}} — one row longer than another. Also empty inner rows mixed with non-empty rows.
Common situations: Data loaded from user-supplied CSV/TSV where a row has a missing or extra field; hand-written literals with a typo; string splitting that produces trailing/missing separators.
Related errors
- Matrix rows and columns must equal in order to find the dete
- index out of range
- index out of bounds
- matrices cannot be multiplied: column count of the first mat
- negative dimensions are not allowed
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/bfc0c9ef2d615ba1.
Report an issue: GitHub.