0

Possible Duplicate:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?

I'm having to build matrices using one of the built in matlab commands and then get a submatrix. For example:

I = eye(10)
I = I(:,2:4)

in order to get columns 2, 3, and 4 of the eye(10) matrix. It's becoming annoying to use two lines and two assignments for something like this, but the following statements are illegal:

I = eye(10)(:,2:4)

and

I = (eye(10))(:,2:4)

Why is this? Is there something in matlab's grammar that would cause ambiguities to arise if such statements were allowed?

Community
  • 1
  • 1
CromTheDestroyer
  • 3,616
  • 3
  • 20
  • 26
  • 1
    Maybe `sub2ind` cannot work on rvalues. Or maybe it was an arbitrary decision the language creators made. There's no real answer to this question. – Praetorian Sep 28 '11 at 21:06
  • The fact that MATLAB uses `()` for both matrix indexing *and* invoking functions is what makes this kind of grammar ambiguous. `eye` could be a function that returns an array which is then indexed, or a function that returns a function handle which is then evaluated with a set of input values, or a variable that you're trying to index in two steps. There are too many ways it could be parsed. – gnovice Sep 29 '11 at 15:23
  • @gnovice: How is it ambiguous? If `eye` returns an array, it should means index. If `eye` returns a function, it should mean function-call. Similarly if `eye` is an array of arrays or an array of functions. Octave parses it fine. – Mechanical snail Oct 17 '11 at 07:28

0 Answers0