is there any way to replace this code part with an equivalent part that runs faster?

1 回表示 (過去 30 日間)
Mohamed Hassan
Mohamed Hassan 2019 年 5 月 21 日
回答済み: Walter Roberson 2019 年 5 月 21 日
yy = 0.5*ones(300,500);
yy_abs = abs_coeff_mat.*yy;
yy_abs_cum=zeros(size(yy_abs));
for i = 2:size(yy_abs,1)
yy_abs_cum(i,:) = sum(yy_abs(1:i,:));
end

回答 (2 件)

Jos (10584)
Jos (10584) 2019 年 5 月 21 日
help cumsum

Walter Roberson
Walter Roberson 2019 年 5 月 21 日
As you appear to be running those in a loop, move the
ones(300,500)
to outside the loop.
You do not appear to be doing implicit expansion, so size(yy_abs)appears to be the same as that 300, 500, so you can probably move the zeros(size(yy_abs)) to outside the loop too.
You can probably replace the loop with a cumsum() followed by zeroing the first row of the result.
I have to say that it looks odd that you would want row 2 to be the sum of row 1 and 2 of yy_abs, and row 3 to be the sum of rows 1, 2, 3 of yy_abs, and so on, but that you want row 1 of the output to be left at zero from the initialization to 0, instead of being the same as the first row of yy_abs.

カテゴリ

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

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by