Matlab外积之类的函数?如何?
我有以下计算,我想在 matlab 中进行矢量化。
我有一个 N x 3 数组,称之为a
。 我有一个 4 x 1 函数句柄元胞数组,将其称为 b
。
我想创建一个 Nx4 矩阵 c
,使得 c(i,j) = b{j}(a(i,:)
。
b< /code> 实际上是一个数组,但我不知道如何以 matlab 能够理解的使用矩阵的格式写下 c 的表示。
I have the following computation I'd like to vectorize in matlab.
I have a N x 3 array, call it a
.
I have a 4 x 1 cell array of function handles, call them b
.
I would like to create an Nx4 matrix c
, such that c(i,j) = b{j}(a(i,:)
.
b
is actually an array, but I don't know how to write down my representation for c in a format that matlab would understand that uses a matrix.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的函数处理数组(即
b{j}(a)
在您的示例中返回 Nx1 数组),您可以使用 CELLFUN 和 CELL2MAT 生成输出数组:如果您的函数仅处理单个行的工作(即
b{j}
需要应用于的每一行a
单独,您可以将 ARRAYFUN 放入混合中(可读性有点受影响不过,基本上,您通过 cellfun 将b
的每个元素通过 arrayfun 应用到a
的每一行):If your function handles work on arrays (i.e.
b{j}(a)
returns a Nx1 array in your example), you can use CELLFUN and CELL2MAT to generate your output array:If your function handles only work on individual rows (i.e.
b{j}
needs to be applied to every row ofa
separately, you can throw ARRAYFUN into the mix (readability suffers a bit though; basically, you're applying each element ofb
via cellfun to each row ofa
via arrayfun):pdist2 几乎解决了上面的问题。也许比我更聪明的人可以弄清楚如何将两者结合在一起。
pdist2 almost solves the problem above. Probably someone more clever than me can figure out how to shoe horn the two together.