need repmat help

Hello,
I have a 4D array with two for loops that is very slow. I know repmat is a good tool to speed things up but I am not very good at using it yet. Is there a way to use repmat to replace the follow set of for loops:
%th_diff3_1 has the following dimensions (621,1405,12,116)
for a = 1:621
for b = 1:1405
x = sum(th_diff3_1(a,b,:,:));
x = sum(x(1,1,:,:));
percent3_1(a,b) = (x/1390) * 100;
end
end
Please help me understand repmat!
Thanks,
Dan

 採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 9 月 19 日

0 投票

It's not about repmat. I think you can do this:
x=sum(th_diff3_1,4);
x=sum(x,3);
percent3_1=(x/1390) * 100

4 件のコメント

Jan
Jan 2011 年 9 月 19 日
It is faster to sum at first over the 3rd dimension and then over the 4th, because this allows a better usage of the cache.
(x/1390)*100 is slowetr than x*(100/1390). In the first case two matrix operations are needed, in the second only one.
Fangjun Jiang
Fangjun Jiang 2011 年 9 月 19 日
Jan, you are amazing! How do you know all of that? I admire you!
Jan
Jan 2011 年 9 月 19 日
@Fangjun: Thanks, but I do not _know_ such details. I just try it taking into account that the calculations are performed by a processor. I've learned programming when efficiency was really important: on a ZX81 with 1kB RAM. It was possible to read the complete operating system in two afternoons.
Although Matlab has a higher complexity, the fundamental rules for efficiency did not change: Keep the data compact to avoid unneded operations.
Dan
Dan 2011 年 9 月 20 日
Fangjun and Jan, thanks so much. I realize now this is not a repmat problem, I just have for loops engrained in my head and need to think differently!

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by