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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With