フィルターのクリア

Arrayfun application to avoid a FOR loop

14 ビュー (過去 30 日間)
Sid
Sid 2015 年 10 月 6 日
移動済み: Dyuman Joshi 2024 年 4 月 6 日
Hi everyone,
The question pertains to the use of arrayfun. I'm interested in using corrcoef on a series of arrays. So far what I have is:
% Create two variables.
alpha = rand([5 6 10]);
beta = rand([5 6 10]);
% Run the loop through to generate the correlation coefficients.
for n = 1:size(alpha,3)
gamma(:,:,n) = corrcoef(alpha(:,:,n), beta(:,:,n));
end
Looking at this, I'm thinking that I should be able to apply arrayfun for cases where the size of the arrays become significantly larger. I tried:
fun = @(A,B) corrcoef(A,B);
iota = arrayfun(fun,alpha,beta)
But all I seem to unfortunately get is a series of arrays with ones.
Would anyone be able to advise on the correct implementation?
Thanks in advance.
  2 件のコメント
Sid
Sid 2015 年 10 月 6 日
移動済み: Dyuman Joshi 2024 年 4 月 6 日
Folks, thank you very much both for your help on the matter. Certainly helped me clear up a lot of questions I had on the implementation of arrayfun.
Mohammad Abouali
Mohammad Abouali 2015 年 10 月 6 日
移動済み: Dyuman Joshi 2024 年 4 月 6 日
You are welcome.
For larger arrays, regular for-loop might be actually faster than arrayfun. If you decided to keep the regular for-loop just make sure to initialize the gamma before entering the loop. (with arrayfun, you don't need to do that)

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

採用された回答

Mohammad Abouali
Mohammad Abouali 2015 年 10 月 6 日
編集済み: Mohammad Abouali 2015 年 10 月 6 日
You can replace
for n = 1:size(alpha,3)
gamma(:,:,n) = corrcoef(alpha(:,:,n), beta(:,:,n));
end
with
gamma=cell2mat(arrayfun(@(n) corrcoef(alpha(:,:,n), beta(:,:,n)), ...
reshape(1:size(alpha,3),1,1,[]), ...
'UniformOutput',false));
Being able to send an operation like this to arrayfun, clearly indicates that the operation is highly parallelizable and one might think that under the hood arrayfun is using some sort of parallelization or multi-threading (at least I was trusting MATHWORKS is doing this). It really doesn't make any sense not to take advantage of such condition.
However, it has brought to my attention that actually once you are using only CPU, arrayfun is no different than writing loop (or it might be even slower).
  2 件のコメント
Edric Ellis
Edric Ellis 2015 年 10 月 8 日
Note that the gpuArray version of arrayfun does run the function in parallel on the GPU - but it can only do this by constraining the functions that can be applied.
Mohammad Abouali
Mohammad Abouali 2015 年 10 月 8 日
Thank you Edric for the information. Could you please elaborate a bit on what you mean by "constraining the function"? I think you have already lots of posts and blogs on this. Would you please share couple of them here. Thank you.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2015 年 10 月 6 日
Your code is going to iterate through the scalar elements of alpha and beta. What you want is to iterate through the pages of the 3-D arrays alpha and beta. Consider what @(n) alpha(:, :, n) does and you should see over which (one) array your ARRAYFUN call should iterate.
[By the way, be careful with those names; both alpha and beta are MATLAB functions. As long as your code doesn't need to use these functions, and doesn't try to "poof" these variables into the workspace, you should be fine, but just keep that in mind.]

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by