user466534
user466534

Reputation:

use array elements as an index

Suppose that we have the following array declaration in matrix

a=[1 2 3;4 5 6;7 8 9]

which looks in matlab as usual matrix form

a =

 1     2     3
 4     5     6
 7     8     9

I am interested how following code works

[a a(a) ]

here is given of course it's answer from a book

 ans =
    1 2 3 1 4 7
    4 5 6 2 5 8
    7 8 9 3 6 9

as I understood first a in bracket simply display original a, or number from 1 to 9, as a second case is use array elements as index into array, so it means that for example numbers

1   2  3
4   5  6
7   8   9 

these are indexes for array a, first index a[1]=1,a{2]=2,a[3]=3 .... a[9]=9 is this right? But why does it print in transpose manner? Should not be like the original matrix? Thanks a lot.

Upvotes: 1

Views: 78

Answers (1)

DWilches
DWilches

Reputation: 23015

In this case the rows are traversed before the columns so a[2] = 4, a[3] = 7 and a[4] = 2

Upvotes: 2

Related Questions