How to add matrices dimension by dimension without using a loop?

1 回表示 (過去 30 日間)
Tintin Milou
Tintin Milou 2015 年 2 月 17 日
コメント済み: Stephen23 2015 年 2 月 17 日
Hi all,
I've got this huge loop that takes a lot of time to be computed:
for i1=1:xN
for j1=1:zN
for i2=1:xN
for j2=1:zN
for j3=1:zN
J(i1,j1,i2,j2,j3) = (ldp(i2,j2,j3) - ld(i1,j1,j2))*pdf_transp(i1,j1,i2,j2,j3);
end
end
end
end
end
xN and zN are 300 and 7 for now, but it easily takes several minutes to compute.
I have tried to use repmat and permute and then do something like this
ld_old = permute(repmat(ld,[1 1 1 xN zN]),[1 2 4 3 5]);
ld_new = permute(repmat(ldp,[1 1 1 xN zN]),[4 5 1 2 3]);
J2 = (ld_new(:)-ld_old(:)).*pdf_transp(:);
That is much faster, but still pretty slow. Is there a more efficient way for this kind of operation?
Thanks!
  1 件のコメント
Stephen23
Stephen23 2015 年 2 月 17 日
Instead of using repmat, you might be able to use bsxfun and avoid this whole business. bsxfun can save a lot memory in these kind of situations, which speed things up.

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

採用された回答

James Tursa
James Tursa 2015 年 2 月 17 日
Don't know if this will be faster than your repmat code, but try this:
J = bsxfun(@minus,reshape(ldp,1,1,i2,j2,j3),reshape(ld,i1,j1,1,j2,1)).*pdf_transp;
  3 件のコメント
James Tursa
James Tursa 2015 年 2 月 17 日
Yes, that was a typo and good catch by you.
Tintin Milou
Tintin Milou 2015 年 2 月 17 日
By the way, do you expect running the bsxfun on CUDA would improve the performance by quite a bit?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by