Matlab外积之类的函数?如何?

发布于 2024-12-15 12:50:45 字数 274 浏览 5 评论 0原文

我有以下计算,我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖哒 2024-12-22 12:50:45

如果您的函数处理数组(即 b{j}(a) 在您的示例中返回 Nx1 数组),您可以使用 CELLFUNCELL2MAT 生成输出数组:

c = cell2mat( cellfun( @(bFun)bFun(a),b,'UniformOutput',false) );

如果您的函数仅处理单个行的工作(即 b{j} 需要应用于 的每一行a 单独,您可以将 ARRAYFUN 放入混合中(可读性有点受影响不过,基本上,您通过 cellfun 将 b 的每个元素通过 arrayfun 应用到 a 的每一行):

c = cell2mat(...
       cellfun( @(bFun)arrayfun(...
            @(row)bFun(a(row,:)),1:size(a,1)),...
       b,'UniformOutput',false) ...
    );

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:

c = cell2mat( cellfun( @(bFun)bFun(a),b,'UniformOutput',false) );

If your function handles only work on individual rows (i.e. b{j} needs to be applied to every row of a separately, you can throw ARRAYFUN into the mix (readability suffers a bit though; basically, you're applying each element of b via cellfun to each row of a via arrayfun):

c = cell2mat(...
       cellfun( @(bFun)arrayfun(...
            @(row)bFun(a(row,:)),1:size(a,1)),...
       b,'UniformOutput',false) ...
    );
—━☆沉默づ 2024-12-22 12:50:45

pdist2 几乎解决了上面的问题。也许比我更聪明的人可以弄清楚如何将两者结合在一起。

pdist2 almost solves the problem above. Probably someone more clever than me can figure out how to shoe horn the two together.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文