rowfun如何知道如何引用表中的变量 [英] How does rowfun know to reference variables inside a table
问题描述
从文档中,我们看到以下示例:>
From the documentation, we see the following example:
g = gallery('integerdata',3,[15,1],1);
x = gallery('uniformdata',[15,1],9);
y = gallery('uniformdata',[15,1],2);
A = table(g,x,y)
func = @(x, y) (x - y);
B = rowfun(func,A,...
'GroupingVariable','g',...
'OutputVariableName','MeanDiff')
将函数func
应用于rowfun
中的A
时,如何知道A
中有名为x
和y
的变量?
When the function func
is applied to A
in rowfun
how does it know that there are variables in A
called x
and y
?
EDIT :我觉得我的最后一个陈述一定不正确,因为如果您执行A = table(g, y, x)
,您将不会获得相同的结果.
EDIT: I feel that my last statement must not be true, as you do not get the same result if you did A = table(g, y, x)
.
对于rowfun
如何使用实际上不使用调用环境中定义的任何变量的函数,我仍然感到困惑.
I am still very confused by how rowfun
can use a function that does not actually use any variables defined within the calling environment.
推荐答案
除非您使用Name/Value参数InputVariables
指定行(及其顺序),否则Matlab只会将第1列作为第一个输入,将第2列作为第一个输入第二个输入等,忽略最终的分组列.
Unless you specify the rows (and their order) with the Name/Value argument InputVariables
, Matlab will simply take column 1 as first input, column 2 as second input etc, ignoring eventual grouping columns.
因此,为了提高代码的可读性和可维护性,我认为始终显式指定InputVariables
是一个好习惯.
Consequently, for better readability and maintainability of your code, I consider it good practice to always specify InputVariables
explicitly.
这篇关于rowfun如何知道如何引用表中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!