返回保存在Matlab多个变量的函数的返回值 [英] Saving return values of function returning multiple variables in Matlab

查看:3946
本文介绍了返回保存在Matlab多个变量的函数的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有使用MATLAB之前,所以原谅这个非常基本的问题。

I have never used matlab before so excuse this very basic question.

基本上,我已经返回多个变量的函数,定义如下所示:

Basically I have a function that returns multiple variables, defined like so:

function [a, b, c]=somefunction(x, y, z)

我知道我能得到的返回值如下:

I know I can get the return values as follows:

[a,b,c] = somefunction(1,2,3);

现在我想要做的,而不是什么 somefunction 的多次运行保存到一个数组,然后以后再取出来。我想:

Now what I would like to do instead is save multiple runs of somefunction into an array and then retrieve them later. I tried:

results = [];
results = [results somefunction(1,2,3)];
results = [results somefunction(4,5,6)];

然后我试图访问各个运行为:

And then I tried accessing the individual runs as:

% access second run, i.e. somefunction(1,2,3) ?
a = results(2, 1);
b = results(2, 2);
c = results(2, 3);

但是这告诉我,该指数超出约束,因为尺寸(结果)= [1,99654] (99654是我需要保存结果的数目) 。所以它不会出现是一个数组?对不起,这个基本的问题,我又从来没有使用MATLAB。

but this tells me that the index is out of bound because size(results) = [1,99654] (99654 is the number of results I need to save). So it does not appear to be an array? Sorry for this basic question, again I have never used matlab.

推荐答案

当你把阵列, [...] ,你是他们连接起来,形成一个长平面数​​组。例如,如果调用1返回3个元素,调用2返回8个元素,并调用3返回4个元素,你会最终有一个14多头排列,也没有办法知道哪些元素从哪个函数调用来了。

When you combine arrays with [ ... ], you're concatenating them, creating one long flat array. For example, if call 1 returns 3 elements, call 2 returns 8 elements, and call 3 returns 4 elements, you'll end up with a 14-long array, and no way of knowing which elements came from which function call.

如果你想保持每次运行结果独立的,你可以在一个单元阵列藏匿其中。你仍然需要在LHS一个逗号分隔的列表来获得所有多个argouts。在 {} -indexing语法,而不是(),啪啪进出细胞元素的含量。

If you want to keep the results from each run separate, you can stash them in a cell array. You still need a comma-separated list on the LHS to get all the multiple argouts. The {}-indexing syntax, as opposed to (), "pops" contents in and out of cell elements.

让我们的结果存储在一个K-乘n阵列名为 X ,其中,函数返回n的输出,我们把它叫做K倍。

Let's store the results in a k-by-n array named x, where the function returns n outputs and we'll call it k times.

x = cell(2, 3); % cell(k, n)
% Make calls
[x{1,1}, x{1,2}, x{1,3}] = somefunction(1,2,3);
[x{2,1}, x{2,2}, x{2,3}] = somefunction(4,5,6);
% Now, the results of the ni-th argout of the ki-th call are in x{ki,ni}
% E.g. here is the 3rd argout from the second call
x{2,3}

您也可以在argouts存储在单独的变量,其可以是更具有可读性。在这种情况下,每一个将第k-长矢量

You could also store the argouts in separate variables, which may be more readable. In this case, each will be a k-long vector

[a,b,c] = deal(cell(1,2));  % cell(1,k)
[a{1}, b{1}, c{1}] = somefunction(1,2,3);
[a{2}, b{2}, c{2}] = somefunction(1,2,3);

当然,这种推广到循环,如果你的 somefunction 输入都服从这一点。

[a,b,c] = deal(cell(1,nIterations));
for k = 1:nIterations
    [a{k}, b{k}, c{k}] = somefunction(1,2,3);
end

详细信息都在DOCO在 http://www.mathworks.com/帮助/ MATLAB /细胞arrays.html DOC细胞

(边注:该结果(1,2)在您的文章应该尺寸为[1,99654]数组成功当然你没做的结果(2,1)?)

(Side note: that results(1, 2) in your post ought to succeed for an array of size [1,99654]. Sure you didn't do results(2, 1)?)

这篇关于返回保存在Matlab多个变量的函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆