0

I was working with simple matrix and got something weird.

A = 
2 3 
5 6 
8 9

And I did this.

A([1 3],[1 2 2]) 

ans = 
2 3 3 
8 9 9

And I did this.

A([1 3],[1 2]) 

ans = 
2 3 
8 9

I can't figure out why MatLab is having that result.

Or, more generally, what does A(input) do in the most general input?

user42459
  • 883
  • 3
  • 12
  • 29
  • 2
    http://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html – Dan Feb 04 '16 at 08:41
  • Possible duplicate of [Linear indexing, logical indexing, and all that](http://stackoverflow.com/questions/32379805/linear-indexing-logical-indexing-and-all-that) – Ander Biguri Feb 04 '16 at 09:27

1 Answers1

5

A(input) generates matrices from the indices that are passed by input. Simple example would be

A(1,1) # select the element from first row, first column
A(2,:) # select the complete second row
A(:,2) # select the complete second column
A([1 2],1) # select the element from the first and second row and first column each

From your example

A([1 3],[1 2 2]) # Select elements from first row and first and two times second column 
                 #as well as third row with first and two times second column.

Similar

A([1 3],[1 2])  # Select elements from first row and first and second column
                # as well as third row and first and second column
Nemesis
  • 2,324
  • 13
  • 23