- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 cellfun
将函数应用于元胞数组中的每个元胞。
我知道我必须设置 'UniformOutput'
每当函数返回非标量值时设置为 false
,以便返回封装在元胞数组中的函数输出。
以下面的元胞数组为例:
C1 = {[1 2 3], [4 5 6]};
C1
有两个单元格,每个单元格包含一个包含三个元素的向量:
C1 =
1×2 cell array
[1×3 double] [1×3 double]
如果我想将 1
添加到每个单元格的内容中,我可以定义函数 @(x) x + 1
并使用 cellfun 应用它
如下:
C2 = cellfun(@(x) x + 1, C1, 'UniformOutput', false);
这非常有效,但请注意,我需要确保将 'UniformOutput'
设置为 false
,如前所述,否则会抛出错误。
然而看完this thread ,我意识到如果我用 cell array construction operator {}
包装函数像这样 @(x) {x + 1}
那么我不需要将 'UniformOutput'
设置为 false
。
因此以下命令将生成与 C2
中相同的结果而不会抛出任何错误:
C3 = cellfun(@(x) {x + 1}, C1);
在代码布局方面,我更喜欢这种方法,因为它比前者更紧凑、更简洁,但我不确定这是否总是安全的。
因此我的问题是:
我能否始终使用 {}
包装函数以避免将 'UniformOutput'
设置为 false
?或者是否存在此类替换不起作用的情况?
我的研究:
help cellfun
'UniformOutput'
-- a logical value indicating whether or not the output(s) ofFUN
can be returned without encapsulation in a cell array. Iftrue
(the default),FUN
must return scalar values that can be concatenated into an array. Iftrue
, the outputs must be of the following types: numeric, logical, char, struct, cell. Iffalse
,cellfun
returns a cell array (or multiple cell arrays), where the (I,J,...)th cell contains the value FUN(C{I,J,...}, ...). When'UniformOutput'
isfalse
, the outputs can be of any type.
以下片段是 an answer 的一部分相关问题:
[...]
cellfun
takes care of the dereference operation which is required to do detailed operations on individual elements of a cell when looping (that is, the{}
) [...]
最佳答案
我可以想到两种使用 cell encapsulation 的场景而不是附加参数 ...'UniformOutput', false)
会导致问题,第一个比第二个更有可能出现:
捕获多个输出:有时您可能希望从正在应用的函数中捕获多个输出,例如调用 unique
在元胞数组的每个元素上并获取额外的索引参数。使用 ...'UniformOutput', false)
我们可以轻松做到这一点:
>> C1 = {[1 2 3], [4 5 6]};
>> [C2, index] = cellfun(@(x) unique(x), C1, 'UniformOutput', false)
C2 =
1×2 cell array
[1×3 double] [1×3 double]
index =
1×2 cell array
[3×1 double] [3×1 double]
但是,当使用单元封装时,这会失败:
>> [C2, index] = cellfun(@(x) {unique(x)}, C1)
Output argument "varargout{2}" (and maybe others) not assigned during call to
"@(x){unique(x)}".
没有输出的函数:我知道你在想什么:“但如果它们不产生输出,那么我就不必担心收集非标量值!” 是的,但我正在想象一个公认的不寻常场景,其中要评估的函数可能是一个附加参数,例如作为参数传递的函数句柄,因此您无法先验地知道 您将处理多少输出。无论如何,对于这种情况,这两种方法是不同的:
>> C1 = {[1 2 3], [4 5 6]};
>> cellfun(@(x) disp(x), C1, 'UniformOutput', false);
1 2 3
4 5 6
>> cellfun(@(x) {disp(x)}, C1);
Error using disp
Too many output arguments.
Error in @(x){disp(x)}
可能您永远不必担心,但我想为了完整起见我会把它包括在内。
关于matlab - 用 {} 运算符包装函数是 'UniformOutput' 的有效替代品,在 cellfun 中是 false 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44102736/
在 Matlab 中,您可以选择创建新的“示例”脚本文件以及脚本、函数、类等。创建它们时,它们会获得一个脚本图标。 它们与其他标准脚本文件的处理方式有何不同? 是否有关于这些示例脚本类型的预期用途的文
我正在运行一个不是我自己编写的大 m 文件,它依赖于某些子函数。我想知道是否在所有嵌套函数的任何地方都使用了特定函数(在我的例子中是函数 eig.m(计算特征值))。有没有快速的方法来做到这一点? 亲
Matlab中有一个函数叫 copulafit .我怎样才能看到这个函数背后的代码?许多 Python 的 numpy 和 scipy 函数在 Github 上很容易开源,但由于某种原因我在 Gith
我定义了一个抽象基类measurementHandler < handle它定义了所有继承类的接口(interface)。这个类的两个子类是a < measurementHandler和 b < me
假设有一个矩阵 A = 1 3 2 4 4 2 5 8 6 1 4 9 例如,我有一个 Vector 包含该矩阵每一列的“类”
我有一个在后台运行的 Matlab 脚本。随着计算的进行,它会不断弹出进度栏窗口。这很烦人。 问题是我没有自己写 Matlab 脚本,这是一段很长很复杂的代码,我不想搞砸。那么如何在不修改 Matla
有没有办法从一个 matlab 程序中检测计算机上正在运行多少个 matlab 进程? 我想要恰好有 n 个 matlab 进程在运行。如果我的数量太少,我想创建它们,如果数量太多,我想杀死一些。您当
我正在测试我们在 Matlab 中开发的一个独立应用程序,当时我注意到它的内存使用量(根据 Windows 任务管理器)达到了 16gb 以上的数倍峰值。我决定在编译版本后面的脚本上使用 profil
我面临着一个相当棘手的问题。在 Matlab 中,命令 S = char(1044) 将俄语字母 д 放入变量 S。但是 disp(S) 返回空白符号,尽管内容实际上是正确的: >> S = char
我在这行 MATLAB 代码中遇到内存不足错误: result = (A(1:xmax,1:ymax,1:zmax) .* B(2:xmax+1,2:ymax+1,2:zmax+1) +
我正在寻找一种在 MATLAB 中比较有限顺序数据与非确定性顺序的方法。基本上,我想要的是一个数组,但不对包含的元素强加顺序。如果我有对象 a = [x y z]; 和 b = [x z y]; 我希
我有一个由 1 和 0 组成的二维矩阵。 mat = [0 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 1 0 0 0 1 0 1 1 0 0 1]; 我需
我可以在 Matlab 中用一组 x,y 点绘制回归线。但是,如果我有一组点(如下图),假设我有四组点,我想为它们绘制四条回归线……我该怎么做?所有的点都保存在 x,y 中。没有办法将它们分开并将它们
我正在尝试使用以下代码在 MATLAB 中绘制圆锥体。但是,当 MATLAB 生成绘图时,曲面中有一个间隙,如下图所示。谁能建议关闭它的方法? clearvars; close all; clc; [
我有一个 map称为 res_Map,包含一组不同大小的数组。我想找到用于存储 res_Map 的总内存。 正如您在下面看到的,看起来 res_Map 几乎不占用内存,而 res_Map 中的各个元素
有没有办法在 MATLAB 中组合 2 个向量,这样: mat = zeros(length(C),length(S)); for j=1:length(C) mat(j,:)=C(j)*S;
已结束。此问题不符合 Stack Overflow guidelines 。它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答它。 关闭 5 年前
我正在尝试将MatLab中的t copula适配到我的数据,并且我的功能是: u = ksdensity(range_1, range_1,'function','cdf'); v = ksdens
大家好,我目前正在尝试使用论文“多尺度形态学图像简化”中的 SMMT 运算符 Dorini .由于没有订阅无法访问该页面,因此我将相关详细信息发布在这里: 请注意,我将相关文章的部分内容作为图片发布。
我在MATLAB中编写代码,需要使用一个名为modwt的函数,该函数同时存在于两个我同时使用的工具箱(Wavelet和WMTSA)中。问题在于,一个版本仅返回一个输出,而另一个版本则返回三个输出。我应
我是一名优秀的程序员,十分优秀!