How to use Matlab's summation function sum
How to use Matlab's summation function sum: 1. Use the "S = sum(A)" method to sum; 2. Use the "S = sum(A,dim)" method to sum according to the specified dimension ;3. Use "S = sum(___,outtype)" to specify the data type of the output result.

The operating environment of this article: Windows 7 system, Matlab R2020a version, Dell G3 computer.
How to use Matlab’s summation function sum:
1. S = sum(A), summation
If x is a vector, calculate the sum of the elements of the vector, such as:
>> x = randi(10,1,10)
x =
7 1 9 10 7 8 8 4 7 2
>> sum(x)
ans =
63If x is an array, calculate the sum of the columns of the array, such as:
>> x = pascal(4,2)
x =
-1 -1 -1 -1
3 2 1 0
-3 -1 0 0
1 0 0 0
>> sum(x)
ans =
0 0 0 -12. S = sum(A,dim), sum according to the specified dimensions
dim = 1, means summing the columns
dim = 2, means summing the rows
dim = 3, means summing the pages
...., and so on
For example:
>> a = toeplitz(1:5)
a =
1 2 3 4 5
2 1 2 3 4
3 2 1 2 3
4 3 2 1 2
5 4 3 2 1
>> sum(a,1)
ans =
15 12 11 12 15
>> sum(a,2)
ans =
15
12
11
12
153, S = sum (___,outtype) specifies the data type of the output result
The optional string parameters are 'double', 'default' and 'native'
Generally speaking, the default is ' default'
Sometimes we sum integers and want the result to be output as a double type, we can use this parameter
For example:
>> a = single(1:4)
a =
1 2 3 4
>> class(sum(a,'double'))
ans =
double
>> class(sum(a))
ans =
singleAlthough the data types of Matlab are relatively Comprehensive, but data type conversion is not completely consistent with other languages, so you need to be careful when using it
4. Whether S = sum(___,nanflag) ignores NaN values
Sometimes when summing, there are NaN values in the data, causing the calculation result to appear NaN. The general processing method is to use the isnan function to exclude nan values. With this option, it is simple
For example :
>> a = rand(1,6);a(randperm(6,2)) = NaN
a =
0.7060 0.0318 NaN 0.0462 0.0971 NaN
>> sum(a)
ans =
NaN
>> sum(a,'omitnan')
ans =
0.8812Related free learning recommendations: php programming (video)
The above is the detailed content of How to use Matlab's summation function sum. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
How to sum irregular merged cells in excel
Jun 25, 2023 pm 02:35 PM
Excel irregular merged cell summing method: 1. Sum function, enter the sum() function and area in each merged cell area; 2. Sumifs function method, the condition area of sumifs cannot be a merged cell, if required Enter the function in the sum area and select Quick Fill Ctrl+enter to implement the sum.
The difference between scilab and matlab
Dec 11, 2023 am 11:13 AM
The difference between scilab and matlab: 1. Annotation symbols; 2. Representation of preset variables; 3. Usage of operators; 4. Definition and calling of matrices; 5. Editing and execution of programs; 6. Data types; 7. Functions Library; 8. Graphical interface; 9. Community support and ecosystem; 10. Cross-platform compatibility; 11. Price. Detailed introduction: 1. Comment symbols. In Scilab, comments are guided by "//", while in Matlab, comments are guided by "%"; 2. Representation of preset variables in Scilab, etc.
How to stop running commands in matlab
Jan 14, 2021 am 11:46 AM
How to stop running commands in matlab: 1. Select a program and click the run icon; 2. Click the pause icon above to temporarily stop the program from running; 3. Click to exit debugging to force stop the busy program.
How to use fprintf in matlab
Sep 28, 2023 pm 04:28 PM
fprintf is a function in MATLAB used to format output. The basic syntax of fprintf is "fprintf(fileID, format, A)", where fileID is an identifier used to specify the file to be written. If you want to write data to the command window, you can use 1 as fileID The value of format is a string used to specify the output format, and A is the data to be output.
What are the Java operations that come with Matlab?
May 03, 2023 pm 04:07 PM
1. Get the full-screen position of the mouse. The upper left corner of the screen is the coordinate origin. To get the mouse position and get the mouse pixel color, it is recommended to use it in conjunction with a while loop or timer function: importjava.awt.MouseInfo;mousepoint=MouseInfo.getPointerInfo().getLocation();mousepoint =[mousepoint.x,mousepoint.y]2 Get the current clipboard content importjava.awt.Toolkitimportjava.awt.datatransfer.DataFlavorclip=
How would you convert MATLAB code to Python code?
Aug 19, 2023 pm 10:53 PM
MATLAB is a popular programming language widely used in engineering and scientific fields, but Python is quickly becoming the language of choice for many programmers due to its flexibility and adaptability. If you want to convert MATLAB code to Python code, it may feel very difficult at first. However, with the right knowledge and approach, you can make the process much easier. Here are some steps to help you convert MATLAB code to Python: Step 1: Familiarize yourself with Python syntax Python and MATLAB have unique syntax, so you need to be familiar with Python syntax before you start converting your code. Spend some time understanding the basics of Python syntax, including variables and data types
How to modify coordinates in matlab
Dec 15, 2023 am 10:40 AM
In MATLAB, you can use the "set" function to modify the axis properties of a graph. Detailed introduction: 1. Modify the range of the coordinate axis: set(gca, 'XLim', [0 10], 'YLim', [0 10]); 2. Modify the label of the coordinate axis: set(gca, 'XLabel', 'My X-axis', 'YLabel', 'My Y-axis'); 3. Modify the scale of the coordinate axis, etc.
How to use matlab griddata function
Dec 15, 2023 am 10:11 AM
The griddata function is used to interpolate the corresponding Z value at a given (X, Y) coordinate, thereby gridding a set of three-dimensional data (x, y, z). Its usage is "griddata(x, y, z, xi, yi, method)".


