# Array indexing order vs dimension number

**URL:** <https://discourse.julialang.org/t/array-indexing-order-vs-dimension-number/15154>\
**Category:** General Usage\
**Created:** [September 19, 2018, 4:33am UTC](https://discourse.julialang.org/t/array-indexing-order-vs-dimension-number/15154 "2018-09-19T04:33:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adriel](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Adriel](https://discourse.julialang.org/u/Adriel)\
**Post date:** [September 19, 2018, 4:33am UTC](https://discourse.julialang.org/t/array-indexing-order-vs-dimension-number/15154/1 "2018-09-19T04:33:34Z")

</div>

This is a general question, I’m not seeking help with anything specific.

The linear algebra convention for matrix indices is (row, column), which is also the case in julia (and matlab). So why does julia (and matlab) assign columns as dimension 1, and rows as dimension 2 for `permutedims` and the like? It seems non-intuitive and inconsistent.

For array dimensions greater than 2, the dimension number and index order line up again - it’s just the first two that have reversed labelling.

I get that arrays are stored column major, and that each consecutive index represents a more “zoomed out” view of the memory layout. But in a high level language that really shouldn’t be very important.

Am I missing some insight? Maybe just a matlab carryover?

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [September 19, 2018, 5:02am UTC](https://discourse.julialang.org/t/array-indexing-order-vs-dimension-number/15154/3 "2018-09-19T05:02:59Z")

</div>

The picture on wikipedia does actually show (row,col) notation, which is common in linear algebra.

---

<div class="post-metadata">

**Author:** ![Juser](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Juser](https://discourse.julialang.org/u/Juser)\
**Post date:** [September 19, 2018, 5:05am UTC](https://discourse.julialang.org/t/array-indexing-order-vs-dimension-number/15154/4 "2018-09-19T05:05:42Z")

</div>

Oops. I didn’t fully read the full question and assumed that OP was wondering why Julia is column major.

---

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [September 19, 2018, 5:31am UTC](https://discourse.julialang.org/t/array-indexing-order-vs-dimension-number/15154/5 "2018-09-19T05:31:25Z")

</div>

This is something that has gotten me confused many times as well.  
AFAICT When we say _column major_ it means that all entries of the first _column_ are a contiguous block of memory and then all other columns in order (+ higher dimensions)  
Therefore the fast changing first index ( dim=1) describes the nth entry within the first column.

This can be confusing as the nth entry in a column refers to a row. So _column major_ only refers to the memory layout.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 19, 2018, 6:42am UTC](https://discourse.julialang.org/t/array-indexing-order-vs-dimension-number/15154/6 "2018-09-19T06:42:57Z")

</div>

I don’t really see what you mean. They seem to me to line up correctly. The first index is row, the second is column, just like in linear algebra, and in every programming language I know of. And the length of the first dimension is equal to the number of rows, the length of the second is equal to the number of columns, and the length of the third dimension is equal to the number of ‘frames’, or whatever you want to call it.

Can you explain in more detail what you mean? And what programming languages do it differently?

**Edit:** OK, I’m not well today, so I missed your point being about `permutedims`, but upon inspection, I don’t see any strange behaviour there either. Do you have an example?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 19, 2018, 6:43am UTC](https://discourse.julialang.org/t/array-indexing-order-vs-dimension-number/15154/7 "2018-09-19T06:43:39Z")

</div>

> [@Adriel](#):
>
> columns as dimension 1, and rows as dimension 2 for `permutedims` and the like? It seems non-intuitive and inconsistent

Perhaps I am missing something, but the reason could be that when `(1, 2)` is permuted, it becomes `(2, 1)`?

---

<div class="post-metadata">

**Author:** ![Adriel](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Adriel](https://discourse.julialang.org/u/Adriel)\
**Post date:** [September 19, 2018, 10:11pm UTC](https://discourse.julialang.org/t/array-indexing-order-vs-dimension-number/15154/8 "2018-09-19T22:11:16Z")

</div>

> And the length of the first dimension is equal to the number of rows, the length of the second is equal to the number of columns, and the length of the third dimension is equal to the number of ‘frames’, or whatever you want to call it.

OK, that’s a great explanation and I’m satisfied, thanks.

The confusion was coming from things like `fft(A,1)` and `fft(A,2)` acting on the columns and rows of A, respectively, which seems reversed. But after thinking about it more carefully, `fft(A,1)` is really a DFT of the “rows”, applied independently over each column. Makes sense.

> OK, I’m not well today, so I missed your point being about `permutedims` , but upon inspection, I don’t see any strange behaviour there either.

No, `permutedims` was just an arbitrary example of a function that considers “dimensions”. Sorry for the red herring.
