简体   繁体   中英

Sum of elements in matrix using sum function by matlab

I have 3 matrices which have similar size. I want to calculate sum of all matrices element by element. How to implement it by matlab using sum function? And If I want to sum only first and third matrix without second matrix, how to do it -Note that my number of matrices can be more than 10? This is my matrix

A(:,:,1)=randint(5,5,10);
A(:,:,2)=randint(5,5,10);
A(:,:,3)=randint(5,5,10);
% Output look like
B=A(:,:,1)+A(:,:,2)+A(:,:,3);
%% How to use sum function for above task

%% If I want to sum only first and third matrix, how to do it?

尝试sum(A,3)沿第三维求和,如果你想省略某些“矩阵”,那么只需使用正态索引:

sum(A(:,:,[1,3]),3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM