如果一个MATLAB函数返回一个可变数量的值,我怎样才能将它们全部作为一个单元阵列? [英] If a MATLAB function returns a variable number of values, how can I get all of them as a cell array?

查看:879
本文介绍了如果一个MATLAB函数返回一个可变数量的值,我怎样才能将它们全部作为一个单元阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function left = remove(cells());我写了一个函数来从单元格数组中删除一些值,如下所示: ,item); 
left = cells {cellfun(@(i)〜isequal(item,i),cells)};

但是当我运行这个时, left has只有第一个值,因为对具有逻辑数组的单元{}的调用将所有匹配的单元格作为单独的值返回。如何将这些单独的返回值分组到单个单元格数组中?



另外,也许已经有办法从单元格数组中移除给定的项目了?我无法在文档中找到它。

解决方案

您必须使用()而不是 {} 来索引单元格:

 函数left = remove(cells,item)
left = cells(cellfun(@(i)〜isequal(item,i),cells));

使用()进行索引会给你使用 {} 的单元子集将返回单元子集的内容,作为以逗号分隔的列表,并且只有该列表的第一个条目将被放置在 left 在您的示例中。



您可以查看这个MATLAB文档了解更多关于使用单元阵列的信息。



< hr>

编辑:评论回应...



如果您有操作最终为您提供逗号分隔列表,您可以通过用花括号将该列表的各个元素放置在单元格数组的单元格中。举个例子,你可以这样做:

  left = {cells {cellfun(@(i)〜isequal(item,i) , 细胞)}}; 

内部花括号组创建 cells 不等于 item ,然后外部集合将这个列表收集到一个单元格数组中。当然,这样做的结果与使用括号进行索引相同,在这种情况下这是更明智的做法。



如果您有函数返回多个输出参数,并且您希望将这些多个值收集到单元数组中,那么它会更复杂一些。您首先必须确定您将获得多少个输出参数,或者您可以使用函数 NARGOUT 以获得所有可能的输出:

  nOut = 3; %#获取前三个输出参数
%#或...
nOut = nargout(@some_fcn); %#从some_fcn获取所有输出参数

然后,您可以将输出收集到1-by- nOut 单元格数组 outArgs 通过执行以下操作:

  [outArgs {1:nOut}] = some_fcn(...); 

需要注意的是 NARGOUT 将返回一个负值,如果函数有一个可变数量的输出参数,因此在这种情况下,您必须自己选择 nOut 的值。

I am writing a function to remove some values from a cell array, like so:

function left = remove(cells, item);
left = cells{cellfun(@(i) ~isequal(item, i), cells)};

But when I run this, left has only the first value, as the call to cells{} with a logical array returns all of the matching cells as separate values. How do I group these separate return values into a single cell array?

Also, perhaps there is already a way to remove a given item from a cell array? I could not find it in the documentation.

解决方案

You have to use () instead of {} to index the cells:

function left = remove(cells, item)
  left = cells(cellfun(@(i) ~isequal(item, i), cells));

Using () for indexing will give you a subset of cells, while using {} will return the contents of a subset of cells as a comma-separated list, and only the first entry of that list will get placed in left in your example.

You can check out this MATLAB documentation for more information on using cell arrays.


EDIT: Response to comment...

If you have an operation that ends up giving you a comma-separated list, you can place the individual elements of the list into cells of a cell array by surrounding the operation with curly braces. For your example, you could do:

left = {cells{cellfun(@(i) ~isequal(item, i), cells)}};

The inner set of curly braces creates a comma-separated list of the contents of cells that are not equal to item, and the outer set then collects this list into a cell array. This will, of course, give the same result as just using parentheses for the indexing, which is the more sensible approach in this case.

If you have a function that returns multiple output arguments, and you want to collect these multiple values into a cell array, then it's a bit more complicated. You first have to decide how many output arguments you will get, or you can use the function NARGOUT to get all possible outputs:

nOut = 3;                   %# Get the first three output arguments
%# Or...
nOut = nargout(@some_fcn);  %# Get all the output arguments from some_fcn

Then you can collect the outputs into a 1-by-nOut cell array outArgs by doing the following:

[outArgs{1:nOut}] = some_fcn(...);

It should be noted that NARGOUT will return a negative value if the function has a variable number of output arguments, so you will have to choose the value for nOut yourself in such a case.

这篇关于如果一个MATLAB函数返回一个可变数量的值,我怎样才能将它们全部作为一个单元阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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