MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何在Matlab中使用匿名函数过滤数组? (https://www.labfans.com/bbs/showthread.php?t=26556)

poster 2019-12-14 20:13

如何在Matlab中使用匿名函数过滤数组?
 
我想写一个过滤数组的函数,最好是匿名函数。很难用语言说出来,但是就像这样:

f = @(x) { if (x > 1) x+1 }; a = [ 1, 2, 3]; f(a) % ==> [ 1 3 4] 我想要的关键点是:
[LIST][*]希望函数接收数字或字符串[*]但是当给定一个数组时,该函数会将其自身应用于该数组中的每个元素并返回另一个数组。[/LIST]这类似于函数log()的工作方式:

>> log(1) ans = 0 >> log([1,2,3]) ans = 0 0.6931 1.0986 谢谢



[B]回答:[/B]

您可以这样做:

f = @(x) x + (x > 1);

[url=https://stackoverflow.com/questions/5250838]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 09:16

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.