Reduce one dimension of a array by using sum

조회 수: 15 (최근 30일)
hung lequang
hung lequang 2012년 10월 19일
Hello,
Can anyone show me how I can reduce one dimension of the following array without using for loops. Thanks!
A=rand(100,100,100);
for ith = 1:100
for jth = 1:100
B(ith,jth) = sum(A(ith,:,jth))
end
end

채택된 답변

Star Strider
Star Strider 2012년 10월 19일
편집: Star Strider 2012년 10월 19일
If you are summing across the second dimension of your three-dimension array A, the easiest way would be to use the ‘dim’ argument of sum:
B = squeeze(sum(A,2))
Does this do what you want?

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 19일
A=rand(100,100,100);
out=sum(A,3)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by