How to remove for-loop in computing a vector of sums?

조회 수: 2 (최근 30일)
Paul Hoffrichter
Paul Hoffrichter 2020년 12월 8일
댓글: Paul Hoffrichter 2020년 12월 8일
How to compute rcross vector without the for-loop?
rcross = zeros( length(elementProduct) - prefixLen , 1 );
for ii = 1: length(elementProduct) - prefixLen
rcross(ii) = sum( elementProduct(ii : ii + prefixLen - 1) );
end
In one scenario prefixLen is 8. length(elementProduct) can vary from a few hundred to a few million.
  댓글 수: 6
Paul Hoffrichter
Paul Hoffrichter 2020년 12월 8일
@KSSV,
Thank you for your comment. If you move it to an answer, I will accept it. Thanks again!
~Paul
Paul Hoffrichter
Paul Hoffrichter 2020년 12월 8일
I think your approach will also work. But I believe (without testing) that using conv would be slower than movsum.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by