matlab中来自单元阵列的平均矩阵(average matrix from cell array in matlab)
我遇到了与细胞阵列有关的问题。 我有一个单元格数组X(大小为400),每个元素都是一个大小为(1x100)的矩阵。 我想获得平均矩阵,以便得到1x100的结果。我尝试了cellfun和之前讨论过的其他方法。 我无法获得理想的结果。 我可以在不将单元阵列转换成矩阵的情况下完成。
I am stuck at a problem related with cell array. I have a cell array X(size-400) with each element as a matrix with size (1x100). I want to get the average matrix so that I get the result as 1x100.I tried cellfun and other methods discussed previously. I am not able to get the desired result. Can I do it without converting the cell array into matrix.
原文:https://stackoverflow.com/questions/38034822
最满意答案
你应该使用list comprehension或
lists:filter
for this case:NewList = [N || N <- List, N > Min]
要么
NewList = lists:filter(fun(N) -> N > Min end, List)
但是,如果你真的更喜欢使用
lists:map
,当N
小于或等于Min
时,你需要考虑返回一些东西。f1(N, Min) when N > Min -> N; f1(N, Min) when N =< Min -> undefined. % return 'undefined' myfun(Min, List) -> lists:map(fun(N) -> f1(N, Min) end, List).
正如您所看到的,您可以通过闭包来“关闭”任意数量的arity函数。
最终会得到一个列表,其中包含所有小于
N
的数字替换为undefined
后跟超过N
。> myfun(5, [1, 2, 4, 6, 7]). > [undefined, undefined, undefined, 6, 7]
lists:map
关注值X -> X'
映射,而不是过滤列表的成员。You should use list comprehension or
lists:filter
for this case:NewList = [N || N <- List, N > Min]
Or
NewList = lists:filter(fun(N) -> N > Min end, List)
However, if you really prefer using
lists:map
, you will need to take into consideration to return something whenN
is less than or equal toMin
.f1(N, Min) when N > Min -> N; f1(N, Min) when N =< Min -> undefined. % return 'undefined' myfun(Min, List) -> lists:map(fun(N) -> f1(N, Min) end, List).
As you can see, you can "close over" any function with any number of arity with closure.
You will end up with a list of with all the numbers less than
N
replaced byundefined
followed by those more thanN
.> myfun(5, [1, 2, 4, 6, 7]). > [undefined, undefined, undefined, 6, 7]
lists:map
concerns with mapping of valuesX -> X'
, not filtering a list's members.
相关问答
更多-
您不能将函数传递给Enum.map ,因为Enum.map将列表中的每个元素作为参数调用您的函数。 如果你使用另一个函数Enum.reduce那么它需要一个2的函数。 这是在列表中的每个项目调用String.downcase/1函数的一个示例: Enum.map(["FOO", "BAR", "BAZ"], fn(x) -> String.downcase(x) end) # String.downcase("FOO") # String.downcase("BAR") # String.downcase( ...
-
将匿名函数作为参数传递给Erlang中的函数?(passing anonymous function as parameter to a function in Erlang?)[2023-08-10]
您可以按照自己喜欢的方式处理。 一开始语法很烦人,但你可能会习惯它。 在任何情况下,您都尝试将值传递给函数,就像执行任何其他参数一样。 如果你牢记这一点,你会发现你有三种方法可以做到这一点。 请考虑以下示例模块: -module(funplay). -export([external/1, internal/1, inline/1]). apply_foo(Foo, Thingy) -> Foo(Thingy). add_one(N) -> N + 1. external(X) -> appl ... -
在scala中获取函数arity(Get function arity in scala)[2022-09-13]
如果I是一个元组,你可以在它上面调用productArity方法,该方法继承自Product 。 如果你有一个你只知道它是某种函数的对象,你将不得不检查(有限的)一组FunctionN类,因为没有像元组那样的常见超类型。 看起来我们目前有Function0到Function22 。 If I is a tuple, you can call the productArity method on it which is inherited from Product. If you have an object ... -
将具有不同arity的函数传递给列表:在Erlang中映射(Passing in a function with different arity to lists:map in Erlang)[2024-01-03]
你应该使用list comprehension或lists:filter for this case: NewList = [N || N <- List, N > Min] 要么 NewList = lists:filter(fun(N) -> N > Min end, List) 但是,如果你真的更喜欢使用lists:map ,当N小于或等于Min时,你需要考虑返回一些东西。 f1(N, Min) when N > Min -> N; f1(N, Min) when N =< Min -> unde ... -
你应该知道Erlang中的导入函数真正做了什么。 这是纯粹的文本转换 。 如果我做了一个-import(foo, [bar/1,baz/2]). 这意味着当我编写类似bar(5)或baz(a, 3)的调用时baz(a, 3)编译器会将这些调用转换为foo:bar(5)和foo:baz(a, 3) 。 这就是它所做的一切,没有别的。 它没有检查任何东西: 它不检查模块foo包含函数bar/1或baz/2 。 它甚至不检查模块foo存在。 它真的只是隐藏你在另一个模块中调用一个函数。 这就是为什么经验丰富的Er ...
-
我最好写一个更全面的回复。 不,没有办法测试实际的宏定义,您只能测试是否已定义具有该名称的宏。 而且您只能测试宏名称,而不能测试具有不同arities的替代宏定义。 这是过去的遗留物,在R13B之前,每个名称只能有一个宏定义。 新的更接近模仿模块中的功能。 执行此操作的“标准”方法是使用一些标志宏来确定要使用的宏/函数集。 例如: -ifdef(DEBUG). -define(DEBUG_PRINT(X), <... something long here ...>). foo(X) -> <... de ...
-
我可以看到你的代码有两个问题: 首先,这一行 def burn_cards(current_hand, cards = []) do 意味着传入的第二个参数必须是空列表,但是您没有传入一个空列表,而是传递一个包含一个元素的列表。 我相信你想用它来代替 def burn_cards(current_hand, cards \\ []) do 这意味着如果省略默认值为空列表,但也接受带有元素的列表。 其次是过滤部分 Enum.filter(current_hand, fn (x, cards) -> ...
-
你在找nargin吗? 例如, >> nargin('genvarname') ans = 2 即函数genvarname的arity为2 Are you looking for nargin? For example, >> nargin('genvarname') ans = 2 i.e. the function genvarname has an arity of 2
-
您只能通过为它们提供一个列表的参数来模拟具有变量arity的函数(匿名或非匿名)。 然后你可以使用apply / 2来评估它。 你的例子可能是(没有经过测试或优化) minn_man(F, Man) -> OnRn = fun (L) -> OnMan = apply(Man, [from_R_to_01( X ) || X<-L]), apply( F, OnMan ) end, Man( [from_R_to_01( X ) || X<-minn( O ...
-
我也刚刚开始使用LFE,但从我收集的内容来看 , 它不支持可变功能 。 这让我觉得数学运算符只接受2个参数。 这个工作的事实: (+ 1 2 3 4 5)让我觉得有一个宏正在运行。 根据这个帖子 ,LFE宏显然可以有任意数量的参数,尽管我还没有深入研究宏。 我的(n00b)建议是:如果您认为需要可变参数函数,请尝试将列表传递给函数而不是变量参数。 如果你仍然认为你需要一个可变函数,那么看看它是否是一个宏。 我怀疑这是一个合理的建议:避免编写宏,除非确实没有更好的选择。 他们使用我曾经使用过的每种语言都难以分 ...