Using Arrays inside arrayfun()

I'm using gpuArrays with arrayfun to speed up calculations, and I would need to pass an array inside the call, as such
[Zn] = arrayfun( @iterFuncDelta,Xo,Yo,orbit)
function [Zn] = iterFuncDelta(xdelta0,ydelta0,refOrbit)
maxIterations=2^15;
delta=complex(xdelta0,ydelta0);
delta0=delta;
n=2;
Zn=refOrbit(1,1)+delta;
while ( n <= maxIterations )
Zn=refOrbit(n,1)+delta;
delta=2*refOrbit(n,1)*delta+delta^2+delta0;
n=n+1;
end
end
How could I pass the array 'orbit' so that it is not treated as a single point (like Xo and Yo) ?
Thanks.

 採用された回答

Matt J
Matt J 2017 年 12 月 23 日
編集済み: Matt J 2017 年 12 月 23 日

0 投票

According to this example, it should be possible with a nested function. Not sure how fast it's likely to be, though.
function Outer(Xo,Yo,orbit) %EDIT
refOrbit=orbit;
function [Zn] = iterFuncDelta(xdelta0,ydelta0)
maxIterations=2^15;
delta=complex(xdelta0,ydelta0);
delta0=delta;
n=2;
Zn=refOrbit(1,1)+delta;
while ( n <= maxIterations )
Zn=refOrbit(n,1)+delta;
delta=2*refOrbit(n,1)*delta+delta^2+delta0;
n=n+1;
end
end
[Zn] = arrayfun( @iterFuncDelta,Xo,Yo);
end

2 件のコメント

MR
MR 2017 年 12 月 23 日
But in that case, how does the arrayfun() call get access to the arrays Xo and Yo?
Matt J
Matt J 2017 年 12 月 23 日
編集済み: Matt J 2017 年 12 月 23 日
You would pass that to Outer() as well (see edit). I of course can't know what other variables you actually have in your workspace, nor how they got there. The inclusion of Outer() was just an example to show the nested context needed for iterFuncDelta.

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2017 年 12 月 23 日

0 投票

3 件のコメント

MR
MR 2017 年 12 月 23 日
編集済み: MR 2017 年 12 月 23 日
This seems to be the other way around than what I need: your article explains how to have extra parameters from the parent function, but I need to have parameters coming from the nested function. From the previous response:
function Outer(orbit)
refOrbit=orbit;
function [Zn] = iterFuncDelta(xdelta0,ydelta0)
[code]
end
[Zn] = arrayfun( @iterFuncDelta,Xo,Yo);
end
Calling Outer(orbit) does not work (the arrayfun() call doesn't see Xo and Yo from the workspace). What am I doing wrong?
Walter Roberson
Walter Roberson 2017 年 12 月 23 日
[Zn] = arrayfun( @(x,y) iterFuncDelta(x,y,orbit), Xo, Yo )
Matt J
Matt J 2017 年 12 月 24 日
I don't think this will work on the GPU.

サインインしてコメントする。

Joss Knight
Joss Knight 2017 年 12 月 26 日

0 投票

You can't do any array or matrix operations in a GPU arrayfun kernel. You can access the contents of an array that is present as an up-level variable such as has been demonstrated in the comments. You can then loop over the contents of the array one element at a time, to emulate array operations such as a dot product. This blog article gives an example of doing this for a simple 3-vector.

カテゴリ

ヘルプ センター および File ExchangeGPU Computing についてさらに検索

質問済み:

MR
2017 年 12 月 23 日

回答済み:

2017 年 12 月 26 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by