【发布时间】:2015-11-18 18:22:37
【问题描述】:
给定
X = [x0,x1,...,xN];
Y = [y0,y1,...,yM];
function result = f(x, y)
... % Cannot use broadcasting. Must take values. Returns a value.
end
我想得到一个矩阵
f(x0,y0) f(x0,y1) ... f(x0,yM)
f(x1,y0) f(x1,y1) ... f(x1,yM)
... ... ... ...
f(xN,y0) f(xN,y1) ... f(xN,yN)
我知道我可以只使用两个嵌套的for 循环,但是有什么东西可以并行化吗?界面类似于arrayfun的东西?
对于那些对f 功能感到好奇的人:
X = [some vector]
W = [some vector]
p(w) % returns a real number; for given vector, returns a vector
g(x, w) % returns value from X; can use broadcasting just like (x .* w).
function result = f(x, y) % takes 2 values from X
result = sum( p( W( g(x,W) == y ) ) );
% | | - boolean vector
% | | - vector of some values from W
% | | - vector of some real values
% | | - a single value
end
【问题讨论】:
-
This可能值得一看。能分享一下f函数的实现吗? -
arrayfun不并行化,它只是编写迭代的另一种方式。至少在最近的 matlab 版本中,我希望嵌套循环至少与任何其他解决方案一样快。除非你使用广播来完成它。 -
仅供参考
arrayfun可以接受多个参数:arrayfun(@(x,y) f(x,y), X,Y) -
@Geoff 是的,但输入必须是相同大小的向量,它会产生一个
f(x0,y0) f(x1,y1) ... f(xN,yN)的向量。完全不同的东西。 -
那么,
g(x,w)是(x .* w)?g是一个函数,对吧?