1

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

so how can i distinguish when will be first traversed rows and when columns?
That is the rule in Matlab. When you use only one index into a bidimensional matrix it will always traverse first the rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.