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?

3
  • What result function will return? Commented Sep 30, 2015 at 7:30
  • @Jonas, I'm not sure that question will help me. The duplicate I just noticed and commented above is more helpful. Commented Sep 30, 2015 at 7:45
  • @Sam: indeed you had found a much better match. That's why I used the link you provided to mark as duplicate. Commented Sep 30, 2015 at 7:47

1 Answer 1

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{:});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.