Suppose I have matrix with elements M(i,j,k) and I want to sum all the elements corresponding to a fixed k, how might I achieve that? In other words I would like to sum over two of the indices. Thank you.

 채택된 답변

Jan
Jan 2012년 4월 15일

1 개 추천

M = randn(10,10,3); % Arbitrary test data
P = M(:, :, 2); % Get submatrix, 3rd dimension is 2
P = P(:); % Reshape submatrix to a column vector
S = sum(P); % Sum over all elements of P

댓글 수: 3

Brooke
Brooke 2012년 4월 15일
Thank you, Jan! Would you mind explaining what each line of code you have written means?
Jan
Jan 2012년 4월 15일
I've added comments. The basics of Matlab are explained in the Getting Started chapters of the documentation.
Brooke
Brooke 2012년 4월 15일
Thank you very much! :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

태그

질문:

2012년 4월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by