英文:
Accessing the first index of an anonymous function
问题
如果我在MATLAB中有一个匿名函数,比如
f = @(t) [t, t+5];
我想创建一个新的匿名函数g,它访问f
的第一个索引,所以在这个例子中,
g = @(t) t;
我该怎么做?
我尝试过类似于以下的操作
g = @(t) f(t)(1);
但编译器说这是“无效的数组索引”,尽管我认为这基本上允许g
是g(t) = t
。
英文:
If I have an anonymous function in MATLAB, say
f = @(t) [t, t+5];
and I wanted to create a new anonymous function g that accesses the first index of f
, so that, in this example,
g = @(t) t;
how can I do so?
I have tried doing something along the lines of
g = @(t) f(t)(1);
but the compiler says that this is "invalid array indexing", even though I thought that this would basically allow g
to be g(t) = t
答案1
得分: 2
我相信我已经解决了这个问题。一个可以做以下操作:
f = @(t) [t, t+5];
first = @(t) t(1);
next = @(t) first(f(t));
英文:
I believe I have resolved this. One can do the following:
f = @(t) [t, t+5];
first = @(t) t(1);
next = @(t) first(f(t));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论