How to get multiple function outputs in a vector?

Let's say I have a function whose outputs consist of two reals a and b

[a,b]=function(c) 

I want to get all the outputs in vector v. v = function (c) does not do what I want, v is "a". Of course, here I could do v = [a, b]. But the function in question is ind2sub for the ND array, so it gives n outputs that I would like to have in the vector directly.

Is there any way to do this? Thank you very much!

+4
source share
1 answer

You can use an array of cells and a comma separated list , for example:

 X = cell(N, 1); [X{:}] = function(C); 

The syntax X{:} actually expanded to [X{1}, X{2}, ...] , which provides a valid receiver for your function. As a result, each output variable will be stored in a different cell in X

If each output variable is a scalar, you can smooth the array of cells into a vector using another comma-separated list extension:

 v = [X{:}]; 
+2
source

Source: https://habr.com/ru/post/1484672/


All Articles