简体   繁体   English

使用matlab求和函数的矩阵元素之和

[英]Sum of elements in matrix using sum function by matlab

I have 3 matrices which have similar size. 我有3个大小相似的矩阵。 I want to calculate sum of all matrices element by element. 我想逐个元素地计算所有矩阵的总和。 How to implement it by matlab using sum function? 如何使用sum函数通过matlab实现它? 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? 如果我只想在没有第二个矩阵的情况下求和第一个和第三个矩阵,那该怎么做 - 我的矩阵数可以超过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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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