Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Adding rows in a Matrix

Subject: Adding rows in a Matrix

From: John

Date: 27 Jan, 2011 11:03:04

Message: 1 of 6

I am novel Matlab user. I have a 2x7 matrix and all I am interested in is to add the elements of each row and get a sum at the end, eg if;

A =

  1.0e+004 *

  Columns 1 through 6

    1.0387 0.1114 0.3500 0.1071 0.0554 0.3968
    0.0003 0.1114 0.3500 0.1071 0.0554 0.3968

  Column 7

    0.1171
    0.1171


I want to get an output of the sum of the first row (1.0387* 1.0e+004 to 0.1171* 1.0e+004) and the sum of (0.0003* 1.0e+004 to 0.1171* 1.0e+004) outputted in a new vector. Once I have the vector I will use min(V) and max(V) to find the extremes, but I also need to backtrack which row elements that corresponded to
the max and min V value. I guess the "size(A,1)" command would inform me about the number of rows I have and therefore I guess that this should let me to be able to backtrack the row elements once min(V) and max(V) has been established.
(For simplicity I have used two rows here, future matrices might be 100 rows)

Yet if I use the sum(A) or sumcum(A) I don't get the sum of each row. Can anyone help me with this?

(I have seen similar things in Simulink but those seem too complicated.)

Subject: Adding rows in a Matrix

From: Krishna Kumar

Date: 27 Jan, 2011 11:19:04

Message: 2 of 6

"John" wrote in message <ihrjd8$ja9$1@fred.mathworks.com>...
> I am novel Matlab user. I have a 2x7 matrix and all I am interested in is to add the elements of each row and get a sum at the end, eg if;
>
> A =
>
> 1.0e+004 *
>
> Columns 1 through 6
>
> 1.0387 0.1114 0.3500 0.1071 0.0554 0.3968
> 0.0003 0.1114 0.3500 0.1071 0.0554 0.3968
>
> Column 7
>
> 0.1171
> 0.1171
>
>
> I want to get an output of the sum of the first row (1.0387* 1.0e+004 to 0.1171* 1.0e+004) and the sum of (0.0003* 1.0e+004 to 0.1171* 1.0e+004) outputted in a new vector. Once I have the vector I will use min(V) and max(V) to find the extremes, but I also need to backtrack which row elements that corresponded to
> the max and min V value. I guess the "size(A,1)" command would inform me about the number of rows I have and therefore I guess that this should let me to be able to backtrack the row elements once min(V) and max(V) has been established.
> (For simplicity I have used two rows here, future matrices might be 100 rows)
>
> Yet if I use the sum(A) or sumcum(A) I don't get the sum of each row. Can anyone help me with this?
>
> (I have seen similar things in Simulink but those seem too complicated.)

WHY NOT USE SUM(A,2)?
It gives the sum of the row.Am I right?

Subject: Adding rows in a Matrix

From: Torsten Hennig

Date: 27 Jan, 2011 11:31:33

Message: 3 of 6

> I am novel Matlab user. I have a 2x7 matrix and all I
> am interested in is to add the elements of each row
> and get a sum at the end, eg if;
>
> A =
>
> 1.0e+004 *
>
> Columns 1 through 6
>
> 1.0387 0.1114 0.3500 0.1071 0.0554
> 0554 0.3968
> 0.0003 0.1114 0.3500 0.1071 0.0554
> 0554 0.3968
>
> Column 7
>
> 0.1171
> 0.1171
>
>
> I want to get an output of the sum of the first row
> (1.0387* 1.0e+004 to 0.1171* 1.0e+004) and the sum of
> (0.0003* 1.0e+004 to 0.1171* 1.0e+004) outputted in a
> new vector. Once I have the vector I will use min(V)
> and max(V) to find the extremes, but I also need to
> backtrack which row elements that corresponded to
> the max and min V value. I guess the "size(A,1)"
> command would inform me about the number of rows I
> have and therefore I guess that this should let me to
> be able to backtrack the row elements once min(V) and
> max(V) has been established.
> (For simplicity I have used two rows here, future
> matrices might be 100 rows)
>
> Yet if I use the sum(A) or sumcum(A) I don't get the
> sum of each row. Can anyone help me with this?
>
> (I have seen similar things in Simulink but those
> seem too complicated.)

Multiply A by ones(size(A,1)) to get
the vector of the sums of the rows.

Best wishes
Torsten.

Subject: Adding rows in a Matrix

From: John

Date: 27 Jan, 2011 12:06:03

Message: 4 of 6

> WHY NOT USE SUM(A,2)?
> It gives the sum of the row.Am I right?

Thank you Krishna. Yes! It works! Now I have been able to establish the max and min of the sums in a vector V. I tried to find a way to output the earlier row for these two extremes, but I am not really sure how to do that. Essentially this is what I am trying to do.

Assume that my vector V = [10 20 50 90]

If I type max(V) Matlab returns 90 and if I type min(V) Matlab returns 10. So far it is all fine. Now I want to print out the entire row that gave me the added sum 90 and 10, respectively. I guess that I need to keep track of the index in the vector but I don't know how. Could you please help? Thank you.

Subject: Adding rows in a Matrix

From: John

Date: 27 Jan, 2011 12:30:09

Message: 5 of 6

Torsten Hennig <Torsten.Hennig@umsicht.fhg.de> wrote in message
> Multiply A by ones(size(A,1)) to get
> the vector of the sums of the rows.
>
> Best wishes
> Torsten.

Thank you kindly Torsten.

Subject: Adding rows in a Matrix

From: Jos (10584)

Date: 27 Jan, 2011 12:41:07

Message: 6 of 6

"John" wrote in message <ihrn3b$fjl$1@fred.mathworks.com>...
> > WHY NOT USE SUM(A,2)?
> > It gives the sum of the row.Am I right?
>
> Thank you Krishna. Yes! It works! Now I have been able to establish the max and min of the sums in a vector V. I tried to find a way to output the earlier row for these two extremes, but I am not really sure how to do that. Essentially this is what I am trying to do.
>
> Assume that my vector V = [10 20 50 90]
>
> If I type max(V) Matlab returns 90 and if I type min(V) Matlab returns 10. So far it is all fine. Now I want to print out the entire row that gave me the added sum 90 and 10, respectively. I guess that I need to keep track of the index in the vector but I don't know how. Could you please help? Thank you.

The HELP system of matlab is pretty good.

try
help min
help max

and learn that they can return two things: the min-/max-imum and the location

hth
Jos

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us