1

I have a MATLAB function which returns a 4-element vector, e.g. [1 2 3 4]. I would like to use that function output to access the corresponding element in an existing 4-dimensional vector, i.e. vec(1, 2, 3, 4). Is there a way to do this without storing the result and then explicitly accessing the elements, as in the following?

result = f(blah);
myElement = vec(result(1), result(2), result(3), result(4));

In my (Python-influenced) head, the answer looks something like this:

result = f(blah);
myElement = vec(*result); % or vec(toSubscripts(result)); or similar

The * operator in Python expands a list into comma-separated arguments. Is there an analogous operator or function in MATLAB that will help solve my problem?

Sam
  • 129
  • 1
  • 2
  • 7

1 Answers1

1

There is something like *result in matlab, it's called comma separated list. Unfortunately you can not create a comma separated list from a array, thus conversion to a cell is first required:

result=(num2cell(f(blah)));
myElement=v(result{:});
Daniel
  • 36,610
  • 3
  • 36
  • 69