访问匿名函数的第一个索引

huangapple go评论53阅读模式
英文:

Accessing the first index of an anonymous function

问题

如果我在MATLAB中有一个匿名函数,比如

f = @(t) [t, t+5];

我想创建一个新的匿名函数g,它访问f的第一个索引,所以在这个例子中,

g = @(t) t;

我该怎么做?

我尝试过类似于以下的操作

g = @(t) f(t)(1);

但编译器说这是“无效的数组索引”,尽管我认为这基本上允许gg(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));

huangapple
  • 本文由 发表于 2023年4月19日 18:19:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定