首页 \ 问答 \ matlab中来自单元阵列的平均矩阵(average matrix from cell array in matlab)

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
更新时间:2022-12-09 19:12

最满意答案

你应该使用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 when N is less than or equal to 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).

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 by undefined followed by those more than N.

> myfun(5, [1, 2, 4, 6, 7]).
> [undefined, undefined, undefined, 6, 7]

lists:map concerns with mapping of values X -> 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( ...
  • 您可以按照自己喜欢的方式处理。 一开始语法很烦人,但你可能会习惯它。 在任何情况下,您都尝试将值传递给函数,就像执行任何其他参数一样。 如果你牢记这一点,你会发现你有三种方法可以做到这一点。 请考虑以下示例模块: -module(funplay). -export([external/1, internal/1, inline/1]). apply_foo(Foo, Thingy) -> Foo(Thingy). add_one(N) -> N + 1. external(X) -> appl ...
  • 如果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 ...
  • 你应该使用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)建议是:如果您认为需要可变参数函数,请尝试将列表传递给函数而不是变量参数。 如果你仍然认为你需要一个可变函数,那么看看它是否是一个宏。 我怀疑这是一个合理的建议:避免编写宏,除非确实没有更好的选择。 他们使用我曾经使用过的每种语言都难以分 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)