52
$\begingroup$

A simple question: By definition, does an m x n matrix have m rows and n columns, or is it vice versa?

$\endgroup$
3
  • $\begingroup$ Yes it's always "{number of rows} by {number of columns}" $\endgroup$ Feb 18, 2015 at 16:15
  • 2
    $\begingroup$ You can name the variables how you like though. Curiously "m by n matrix" is about twice as common as "n by m matrix" in Google search results. $\endgroup$ Feb 18, 2015 at 16:19
  • 2
    $\begingroup$ @ColonelPanic, that's probably because for a matrix $A$ operating on an $n$ dimensional vector $\mathcal{x}$ (i.e. $A \mathbf{x} = \mathbf{y}$) $\mathbf{y}$ is $m$ dimensional. In other words, it puts the input dimension before the output dimension alphabetically. $\endgroup$
    – Shep
    Apr 3, 2015 at 1:42

4 Answers 4

48
$\begingroup$

An $m \times n$ matrix has $m$ rows and $n$ columns.

$\endgroup$
9
  • $\begingroup$ can you provide a reference/citation for this? $\endgroup$ Sep 6, 2012 at 2:16
  • 3
    $\begingroup$ All the textbooks i have read (both cs and math) have used this notation. For example, Strang's Introduction to Linear Algebra 4th. $\endgroup$
    – James
    Sep 6, 2012 at 2:17
  • $\begingroup$ You said "almost all". Were there any exceptions? $\endgroup$ Sep 6, 2012 at 2:19
  • 1
    $\begingroup$ Sorry, I meant all. $\endgroup$
    – James
    Sep 6, 2012 at 2:20
  • 1
    $\begingroup$ @IvanBalashov In Numpy the first dimension is the row, not the column. $\endgroup$
    – bfontaine
    Oct 22, 2016 at 7:36
1
$\begingroup$

I suggest you always to check the notation on the book which you are using. I found sometimes this notation with different meaning. In advanced books, for example. Even the notation for linear maps as matrices. Sometimes they write $xT$.

$\endgroup$
2
  • $\begingroup$ What does xT refer to in this case? $\endgroup$ Sep 6, 2012 at 2:36
  • $\begingroup$ It is the notation for the image of $x$ by the linear map $T$. Usually we write $T(x)$ or $Tx$. $\endgroup$
    – Sigur
    Sep 6, 2012 at 2:38
1
$\begingroup$

Always check and make sure you have the right convention for the occasion. Usually m x n is rows x columns. I like to remember this as being in REVERSE alphabetical order - Rows by Columns, or R first then C. However, in Boyce & DiPrima's book "Elementary Differential Equations and Boundary Value Problems" an m x n matrix has m vertical columns and n horizontal rows.
However, when addressing elements within a matrix, it's the opposite. The element "a sub i,j" references the element in the ith row and jth column. Lesson? Always check to make sure you have the correct convention!

$\endgroup$
2
  • 1
    $\begingroup$ so much for the "universal language of mathematics" :( $\endgroup$ Jan 21, 2019 at 17:32
  • $\begingroup$ @RobertLugg exactly. x columns by y rows. i columns by j rows. Then algebra with the 'hold my beer' goes with m rows by n columns. I almost have feelings about this... $\endgroup$ Apr 19 at 13:15
1
$\begingroup$

Yes... It's m-rows and n-Columns.
Here is an example, how you can generate and read a matrix in JavaScript :)

let createMatrix = (m, n) => {
  let [row, column] = [[], []],
      rowColumn = m * n
  for (let i = 1; i <= rowColumn; i++) {
    column.push(i)
    if (i % n === 0) {
      row.push(column)
      column = []
    }
  }
  return row
}

let setColorForEachElement = (matrix, colors) => {
  let row = matrix.map(row => {
    let column = row.map((column, key) => {
      return { number: column, color: colors[key] }
    })
    return column
  })
  return row
} 

const colors = ['red', 'green', 'blue', 'purple', 'brown', 'yellow', 'orange', 'grey']
const matrix = createMatrix(6, 8)
const colorApi = setColorForEachElement(matrix, colors)

let table ='<table>'
colorApi.forEach(row => {
  table+='<tr>'
    row.forEach(column =>  table +=`<td style='background: ${column.color};'>${column.number}<td>` )
  table+='</tr>'
})

document.write(table);
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .