How to use vectorization to sum every five elements in 100x1 vector?

12 ビュー (過去 30 日間)
mrbond99
mrbond99 2020 年 6 月 26 日
コメント済み: mrbond99 2020 年 6 月 27 日
I have tried this code..
c = (1:100)'; % Dummy data
for n = 1:20 % This one works
m(n) = 1 + (n - 1)*5;
p(n) = 5 + (n - 1)*5;
csum(n,:) = sum(c((m(n):p(n)),:));
end
nn = 1:20; % The vectorization code adds only the first five elements.
mm(nn) = 1 + (nn-1)*5;
pp(nn) = 5 + (nn-1)*5;
ccsum(nn,:) = sum(c((m(nn):p(nn)),:));
The result that I want is the one like csum but for vectorization's result, ccsum is only at the sum of the first five elements in c. Please help..

採用された回答

Walter Roberson
Walter Roberson 2020 年 6 月 26 日
csum = sum( reshape(c, 5, []) ).' ;

その他の回答 (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