Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of elements in matrix using sum function by matlab

Tags:

matrix

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?
like image 357
Jame Avatar asked Nov 18 '25 08:11

Jame


1 Answers

Try sum(A,3) to sum along the third dimension and if you want to leave out certain "matrices" then just use normal indexing:

sum(A(:,:,[1,3]),3)
like image 123
Dan Avatar answered Nov 19 '25 22:11

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!