- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用单元格来管理我正在处理的一些内容中的数据。我希望能够执行以下操作:
A = cellfun( @(X)( randn( 5,5 ) ), cell( 5,1 ), 'UniformOutput', 0 );
B = cellfun( @(X)( randn( 5,5 ) ), cell( 5,1 ), 'UniformOutput', 0 );
%#
%# Each of the following would fail if cell member dimensions
%# don't match up
%#
%# matrix sums for each cell entry
%# C = A + B;
C = cellfun( @(X,Y)( X + Y ), A, B, 'UniformOutput', 0 );
%#
%# direct/hadamard product
%# D = A .* B;
D = cellfun( @(X,Y)( X .* Y ), A, B, 'UniformOutput', 0 );
%#
%# matrix-matrix products (not across all entries)
%# E = A * B;
E = cellfun( @(X,Y)( X * Y ), A, B, 'UniformOutput', 0 );
但是,我不想使用极其冗长的语法来完成它。当我只想为单元格上的数学运算符提供定义时,为此创建一个新类似乎有点过分。
问题:上课是解决问题的唯一方法吗?
如果我编写一个类来执行此操作,那么编写代码肯定会更容易。我看到的最大的负面影响与优化有关,尽管还有一些其他的事情让我感到烦恼......
任何在幕后进行的优化(例如,当 Jacket 编译某些东西以在 GPU 上运行时)可能会更难确定要进行哪些优化。例如,假设我有两个单元格 (A,B),其中包含许多适当维数的矩阵。如果我编写代码来生成一个新单元格:
Z = c1*A + c2*B
...使用标量 {c1,c2},我可以用 Jacket(或其他任何东西)很容易确定它应该按照以下方式进行计算的方式来编写它:
Z{kk} = c1*A{kk} + c2*B{kk}
或者可能是比这更好的优化。否则。它最终可能会变得更慢和/或内存效率更低,例如:
temp1 = cellfun( @(X)( c1*X ), A );
temp2 = cellfun( @(X)( c2*X ), B );
Z = cellfun( @plus, temp1, temp2 );
假设 MATLAB 或 Jacket 无法对其进行优化,这最终会使用过多的内存。
最佳答案
事实上,可以为 MATLAB 中的内置数据类型创建新的运算符或重载现有的运算符。我在 my answer 中描述了一个例子另一个关于 modifying the default overflow behavior of integer types 的问题.
首先,您可能想要查看元胞数组目前存在哪些方法。您可以使用函数 METHODS 执行此操作,这是我在 MATLAB R2010b 中得到的:
>> methods cell
Methods for class cell:
aa2nt issorted regexptranslate strfind
accumarray newdepfun reshape strjust
cell2struct nt2aa rna2dna strmatch
ctranspose nwalign seq2regexp strtok
display permute setdiff transpose
dna2rna regexp setxor union
intersect regexpi sort unique
ismember regexprep strcat
算术运算符方法将在上面的列表中显示为它们的 function equivalents ,例如 +
运算符的 plus
或 .*
运算符的 times
。仅为元胞数组定义了 transpose
方法(.'
运算符)。您必须自己创建其余部分,定义给定运算符对元胞数组参数的行为方式。
您可以先创建一个名为@cell
的新文件夹,然后将其放在您的MATLAB path 上的现有文件夹中。 .然后将新方法放在 @cell
文件夹中。例如,元胞数组的 plus
方法的非常简单实现(没有任何输入检查、错误检查等)将是这样的:
function C = plus(A,B)
C = cellfun(@plus,A,B,'UniformOutput',false); %# Apply plus cell-wise
end
在上面的代码中,您可能首先要检查操作数 A
和 B
是否是大小相同的元胞数组。但是,您可以创建任何您想要的独特功能,例如允许 B
成为一个标量值,该值将添加到 A
的每个单元格中。完全由您来定义 +
运算符对元胞数组的行为方式。
这将允许您以更紧凑的方式编写代码,如本例所示:
>> A = {[1 2 3] [4 5] 6}; %# One 3-element cell array
>> B = {5 [4 5] 2}; %# Another 3-element cell array
>> C = A+B; %# Use the new plus operator
>> C{:} %# Display the cell contents
ans =
6 7 8
ans =
8 10
ans =
8
我真的无法谈论幕后优化以及这可能会如何影响它们。我知道文档 "Techniques for Improving Performance"特别提到关于 overloading built-in functions :
Overloading MATLAB built-in functions on any of the standard MATLAB data classes can negatively affect performance. For example, if you overload the
plus
function to handle any of the integer classes differently, you may hinder certain optimizations in the MATLAB built-in function code forplus
, and thus may slow down any programs that make use of this overload.
但是,在您的情况下,您没有为类重载现有 函数。您只是在创建该类不存在的新类,因此很难说这最终会对性能产生什么影响。
关于MATLAB:是否可以在 native 构造(单元、结构等)上重载运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5365464/
谁能解释一下原因: (define a (lambda() (cons a #f))) (car (a)) ==> procedure ((car (a))) ==> (procedure . #f)
这是 PyBrain 网站的摘录。我了解大部分正在发生的事情,但是一行让我完全难住了。我以前从未在 python 代码中看到过这样的东西。这是整个循环,对于上下文: for c in [0,
我是gradle / groovy的新手。我想创建将做一些事情的自定义任务。我的第一个问题是任务完成时该如何做?我可以覆盖doFirst / doLast闭包吗?也许我可以重写某些在开始和结束时都会执
我刚刚开始评估 MS 企业库。他们使用以下指令来获取实例: var customerDb = EnterpriseLibraryContainer.Current.GetInstance("C
这是我的 if else Ansible 逻辑.. - name: Check certs exist stat: path=/etc/letsencrypt/live/{{ rootDomain
我正在使用construct 2.8 对一些失传已久的 Pascal 程序创建的一些文件的 header 进行逆向工程。 header 由许多不同的记录组成,其中一些是可选的,我不确定顺序是否固定。
我在将 getchar() 的输入放入 char *arr[] 数组时遇到问题。我这样做的原因是因为输入数据(将是一个带有命令行参数的文件)将存储在一个 char 指针数组中以传递给 execvp 函
通常我们不能约束类型参数 T派生自密封类型(例如 struct 类型)。这将毫无意义,因为只有一种类型适合,因此不需要泛型。所以约束如下: where T : string 或: where T :
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
#include using namespace std; class A { private: int m_i; friend int main(int argc, char cons
这个问题在这里已经有了答案: Are there legitimate uses for JavaScript's "with" statement? (33 个答案) 关闭 9 年前。 我有这个代
在this answer我看到了下一个 Bash 结构。 yes "$(< file.txt)" 什么意思 "$(< file.txt)" ? 我明白了 命令替换 - $(command)用命令的结
if (a == 1) //do something else if (a == 2) //do something else if (a == 3) //do somethi
关于构造的快速简单的问题。 我有以下用于将项目添加到 ListView 的代码。 ListViewItem item = new ListViewItem(); item.Text = file; i
我想使用 std::vector 来控制给定的内存。首先,我很确定这不是好的做法,但好奇心占了上风,无论如何我都想知道如何做到这一点。 我遇到的问题是这样的方法: vector getRow(unsi
下面显示了一段简单的javascript: var mystring = ("random","ignored","text","h") + ("ello world") 这个字符串会生成 hello
在 Java 中,创建对象的标准方法是使用 MyClass name = new MyClass(); 我也经常看到构造 new MyClass() { /*stuff goes in here*/
我正在编写 C++ ndarray 类。我需要动态大小和编译时大小已知的数组(分别分配自由存储和分配堆栈)。我想支持从嵌套的 std::initializer_list 进行初始化。 动态大小的没问题
我正在将一个项目从 Visual Studio 2005 转换为 Visual Studio 2008,并提出了上述结构。 using Castle.Core.Resource; using Cast
我想知道我在这里的想法是否正确,我主要针对接口(interface)进行编程,所以我想知道下面的类是否应该通过 DI 注入(inject),或者我应该自己实例化一个类... 注意:这些服务保存在我的核
我是一名优秀的程序员,十分优秀!