0

Consider the following simple example.

test=[1,2,3;4,5,6];
size(test)

we will recieve 2 3, which means test's dimension is 2x3. I expect 2 3 being a (1x2)-array data itself. So it should be possible to ask size(test)(2), but then you will get an error; Error: Invalid array indexing.. Using size(test)(1,2) also raises the same error message. However, if you save this size's result by a name, then you won't get an error using the indexing and the name.

test2=size(test);
test2(2) % you will get 3.
test2(1,2) % you will get 3.

Sometimes you may only need this dimension once and saving it is a waste of memory. Why isn't it possible to ask size(test)(2) but it is possible to ask test2(2)?

  • 3
    Hint: you can do `size(test,2)`. But the short answer is: MATLAB returns by value (generally speaking, there are JIT compiler nuances, but assume it does), and the return needs to be stored. `size()` is a function. Just how the language is designed. Also, saving it is not a waste of memory (aside from it being absurdly small memory), because not saving it also implies having it on RAM at some point, otherwise you would not be able to index it. – Ander Biguri Jul 19 '21 at 09:42
  • 1
    It is _possible_ but not with the easy syntax you are proposing. To see all the ways to achieve this indexing, I recommend you read the second most voted question for the MATLAB tag: [How can I index a MATLAB array returned by a function without first assigning it to a local variable?](https://stackoverflow.com/q/3627107/3460361). Don't forget to also read the various answers ... – Hoki Jul 19 '21 at 10:13

0 Answers0