Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
5.3.0 - 日本語

Change language to:
English - Français - Português

Please note that the recommended version of Scilab is 2025.0.0. This page might be outdated.
See the recommended documentation of this function

Scilab manual >> Matlab to Scilab Conversion Tips > Matlab-Scilab equivalents > S > sum (Matlab function)

sum (Matlab function)

Sum of array elements

Matlab/Scilab equivalent

Matlab Scilab

Particular cases

M=sum(A)

Scilab sum(A) returns the sum of all components of A. So, if A is a vector, then Scilab and Matlab work in the same way. If A is a matrix, Scilab sum(A) gives the sum of all elements of A but Matlab returns the sum of each column. Finally, if A is a multidimensional array, Matlab works on the first non-singleton dimension of A what Scilab does not. So, to be sure to find a Scilab equivalent for Matlab call to sum it is better to precise dimension on which to work.

M=sum(A,dim)

In Scilab dim=1 is equivalent to dim="r" and dim=2 is equivalent dim="c". In Matlab, dim can be greater then the number of dimension of A (in this case, M=A), in Scilab you will get an error message.

Examples

Matlab Scilab
A = [1,2,3;4,5,6]
M = sum(A)
M = [5,7,9]
M = sum(A,1)
M = [5,7,9]
A = [1,2,3;4,5,6]
M = sum(A)
M = 21
M = sum(A,"r")
M = [5,7,9]
<< struct (Matlab function) S surf (Matlab function) >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Wed Jan 26 16:25:14 CET 2011