【发布时间】:2018-06-25 10:22:36
【问题描述】:
如题,作为matlab函数输出的元胞数组如何在不使用临时数组的情况下直接变成逗号分隔的列表?
也就是说,我知道你会写
% functioning code
tmp = cell(1,3); % function that makes a temporary cell_array;
b = ndgrid(tmp{:}); % transform tmp into a
% comma-separated list and pass into another function
我正在寻找一种允许我以类似的方式执行此操作的方法
% non functioning code
b = ndgrid( cell(1,3){:} );
以便它可以在匿名函数中使用,其中不允许使用临时参数。示例:
fun = @(x)accept_list( make_a_cell(x){:} );
如何做到这一点? 我认为在使用运算符'{:}'时必须调用一个函数,但它会是哪个?
编辑澄清:
该问题被标记为可能重复的答案中的解决方案不能解决问题,因为在创建逗号分隔列表时 subsref 不能替代 {:}。
例子:
a = {1:2,3:4}
[A1,A2] = ndgrid(subsref(a, struct('type', '{}', 'subs', {{':'}})));
是(错误地)
A1 =
1 1
2 2
A2 =
1 2
1 2
但是
a = {1:2,3:4}
[A1,A2] = ndgrid(a{:});
返回(正确)
A1 =
1 1
2 2
A2 =
3 4
3 4
【问题讨论】:
-
为什么需要单线?您认为您会获得哪些额外优势?
-
我在问题下方的编辑中添加了一些说明。不幸的是,建议的解决方案不起作用。
-
所以创建一个临时匿名函数来调用实际的匿名函数。我认为没有任何真正需要单线