how can i do that for all columns?

1 次查看(过去 30 天)
Ach
Ach 2018-3-5
编辑: Stephen23 2018-3-13
hi , i have a matrix (length(a),length(b)) and i want to write a function that calculates the sum of all a in a column. how can i do that for all columns?

回答(1 个)

Suraj Mankulangara
Suraj Mankulangara 2018-3-13
Hi Ach
The "sum" function in MATLAB returns the total of all elements in a column (or row) of a matrix. For more information on how to use the sum function, you can refer to the link below:
https://in.mathworks.com/help/matlab/ref/sum.html
By default, the "sum" function computes column-wise total of elements in a matrix, so in your case,
sum(matrix)
should get you the result that you want.
  1 个评论
Stephen23
Stephen23 2018-3-13
编辑:Stephen23 2018-3-13
although to be safe, in case the size of the matrix might change in future:
sum(matrix,1)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by