How to Get the Sum of the Rows in a Matlab Matrix


Using the MathWorks product Matlab R2014b to solve math problems is fast and easy. Matlab was designed for all sorts of mathematical computations, including engineering, controlling systems, communications, signal processing, financial modeling and data analysis. In Matlab, data manipulation is performed with matrices. One of the basic matrix operations you must master before moving on to complex computations is how to find the sum of a row.

How to Sum a Matrix Row in Matlab

  • Define the matrix whose rows you want to sum. Type

    A = [1 2 3; 4 5 6; 7 8 9]

    in the Command Window and type "A" without the quotations in the command line to define the following example matrix.

    A =

          1     2     3
    4 5 6
    7 8 9
  • Select the equation "S = sum(A,dim)" from the Matlab library. In this equation, "dim" stands for the dimension you want to sum. It must be a positive integer scalar.

  • Type the summation equation into the command line. Use "2" for the "dim" value to sum rows.

    S = sum(A,2)

    For our example matrix, this is the result of the summation equation.

    S =


Tips & Warnings

  • If you change the "dim" value to 1, i.e., S = sum(A,1), you will get the sum of the columns instead of the rows.
  • Do not forget to enter a value for "dim"; otherwise you will get a sum of the columns by default.


  • Photo Credit George Doyle/Stockbyte/Getty Images
Promoted By Zergnet


You May Also Like

Related Searches

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!