可能的重复:
如何索引函数返回的 MATLAB 数组而不先将其分配给局部变量? https://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it
我想对函数的输出使用索引。我使用 textscan 函数读取非常大的文本文件(15 GB)。在我的例子中,textscan 函数的返回是一个 1x1 元胞数组,其中包含一个非常大的数值数组。
而不是这样做:
tmp = textscan(...);
final_result = mat2cell(tmp{1,1});
我想要做:
final_result = mat2cell( textscan(...){1,1} );
如果这可行,就可以避免创建非常大的临时变量 tmp。还有其他方法可以避免临时变量吗?
如果您仍然想知道,请考虑以下示例:
%# some function that returns a cell array (TEXTSCAN in your case)
myFunc = @() {rand(5,5)};
%# normally you would write
C = myFunc();
C = C{1,1};
这是 cellarray 版本@gnovice https://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-without-first-assigning-it-to-a-local-variable/3628885#3628885回答链接的问题(丑陋但有效):
%# equivalent to: C = myFunc(){1,1}
C = subsref(myFunc(), struct('type','{}','subs',{{[1 1]}}))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)