首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >笛卡尔乘积的Matlab矢量化

笛卡尔乘积的Matlab矢量化
EN

Stack Overflow用户
提问于 2013-03-07 03:47:29
回答 2查看 352关注 0票数 0

假设我有两个数组X和Y,我需要一个矩阵M(i,j) = some_func(X(i),Y(j))。我怎样才能在不使用循环的情况下得到它呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-07 05:07:11

最好的答案是使用bsxfun,如果它是一个选项。根据bsxfun的帮助,它可以在任何通用的二元函数上工作,只要:

代码语言:javascript
复制
  FUNC can also be a handle to any binary element-wise function not listed
    above. A binary element-wise function in the form of C = FUNC(A,B)
    accepts arrays A and B of arbitrary but equal size and returns output
    of the same size. Each element in the output array C is the result
    of an operation on the corresponding elements of A and B only. FUNC must
    also support scalar expansion, such that if A or B is a scalar, C is the
    result of applying the scalar to every element in the other input array.

如果你的函数只接受标量输入,那么循环是一个简单的选择。

票数 4
EN

Stack Overflow用户

发布于 2013-03-07 03:53:12

很难回答你模糊的问题,这最终取决于你的功能。您可以执行的操作是使用meshgrid,然后执行操作,通常使用点运算符

例如:

代码语言:javascript
复制
x = 1:5;
y = 1:3;

[X,Y] = meshgrid (x,y)

M = X.^Y;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15256609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档