Sum over a dimension

12 ビュー (過去 30 日間)
JvdS
JvdS 2019 年 8 月 5 日
回答済み: madhan ravi 2019 年 8 月 5 日
Hi,
I try to sum over the third dimension of a matrix A, say A = ceil(5*rand(4,4,3)).
The third dimension equals variation in time. For t= 0 I want to have A(:,:,1); for t= 1 I want to have A(:,:,1) + A(:,:,2); for t=3 I want to have A(:,:,1) + A(:,:,2) + A(:,:,3).
The code below works, however, I want to do it without for-loops. Can someone help me?
[nr, nc, nd]= size(A)
for i=1:nd
for ii= 1:nr
for jj=1:nc
B(ii,jj,i) = sum(reshape(A(ii,jj,[1:i]),i,1)));
end
end
end

採用された回答

madhan ravi
madhan ravi 2019 年 8 月 5 日
B = cumsum(A,3)

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by