Thread Subject: Help needed - Adding up combinations of elements in a matrix.

Subject: Help needed - Adding up combinations of elements in a matrix.

From: Drew

Date: 16 Nov, 2009 21:27:02

Message: 1 of 7

I have a sample matrix.

A = [ 0, 10, 12;9, 0, 6;5 8 0]

I wanted to add up all possible combinations of an element from each row of a 3x3 matrix. For example, (1,1) +(2,1)+(3,1) or (1,2)+(2,3)+(3,2). The diagonal of the matrix are zeros, and any computation used with these zeros would not be anywhere close to my desired sum of the different elements from each row and would not matter. So, (1,1), (2,2) and (3,3) could be discarded very easily, leaving (1,2),(1,3),(2,1),(2,3),(3,1),(3,2) as my elements for possible combinations.

My initial approach noticed that on every sum of one element from each row, my row number increases by 1 each time (obviously), so I could incoporate a loop to assist me with this. However, the difficulty I am having is the column number. I can't seem to get a solid foundation on getting all possible combinations, even though the number of columns is only going to vary from 1 to 3.

Overall, my goal is to look at all the sums of the possible combinations of one element from each row, match it to a target integer, and then get the element positions (r,c) for each of those values. I need to use a for or while loop to do this for my specific task, so as soon as the target is reached, I can break out of the loop.

Thank you in advance for the assistance.

Subject: Help needed - Adding up combinations of elements in a matrix.

From: Drew

Date: 16 Nov, 2009 21:33:03

Message: 2 of 7

I really would just like to see an example of how you could accomplish adding up the different combination of elements (one from each row) in a matrix using a loop.

Thank you again.

Subject: Help needed - Adding up combinations of elements in a matrix.

From: someone

Date: 16 Nov, 2009 22:00:03

Message: 3 of 7

"Drew " <drewtoid@gmail.com> wrote in message <hdsg36$5ve$1@fred.mathworks.com>...
> I have a sample matrix.
>
> A = [ 0, 10, 12;9, 0, 6;5 8 0]
>
> I wanted to add up all possible combinations of an element from each row of a 3x3 matrix. For example, (1,1) +(2,1)+(3,1) or (1,2)+(2,3)+(3,2). The diagonal of the matrix are zeros, and any computation used with these zeros would not be anywhere close to my desired sum of the different elements from each row and would not matter. So, (1,1), (2,2) and (3,3) could be discarded very easily, leaving (1,2),(1,3),(2,1),(2,3),(3,1),(3,2) as my elements for possible combinations.
>
> My initial approach noticed that on every sum of one element from each row, my row number increases by 1 each time (obviously), so I could incoporate a loop to assist me with this. However, the difficulty I am having is the column number. I can't seem to get a solid foundation on getting all possible combinations, even though the number of columns is only going to vary from 1 to 3.
>
> Overall, my goal is to look at all the sums of the possible combinations of one element from each row, match it to a target integer, and then get the element positions (r,c) for each of those values. I need to use a for or while loop to do this for my specific task, so as soon as the target is reached, I can break out of the loop.
>
> Thank you in advance for the assistance.

I want to help, but I have no idea of what you are asking for.
What does "add up all possible combinations of an element from each row" mean?
In your example, (1,1) +(2,1)+(3,1) is indeed the sum of a row.
But, how is (1,2)+(2,3)+(3,2) the sum of a row?
(Can you give another example?)
What is the difference between what you are asking and what

B = sum(A,2)

does? See

doc sum

Subject: Help needed - Adding up combinations of elements in a matrix.

From: Doug

Date: 16 Nov, 2009 23:27:01

Message: 4 of 7

q=[1 2 3;3 2 1; 4 4 4];
mysum=0;
for r=1:1:3
   for c=1:1:3
      mysum=mysum+(r,c);
   end
end

or as stated before, use the sum function

sum(sum(q))

Subject: Help needed - Adding up combinations of elements in a matrix.

From: Matt Fig

Date: 17 Nov, 2009 00:00:20

Message: 5 of 7

Unless I misunderstand, I think the OP is talking more like this (a rough draft that gets the job done, no optimizations added):

A = [ 0, 10, 12;9, 0, 6;5 8 0];

F = perms(1:3);
G = npermutek(1:3,3);
T = 0;
for ii = 1:size(F,1)
    for jj = 1:size(G,1)
        B = A(F(ii,1),G(jj,1));
        C = A(F(ii,2),G(jj,2));
        D = A(F(ii,3),G(jj,3));
        if all([B,C,D])
            T = T + sum([B,C,D]);
        end
    end
end


Where npermutek is at:
http://www.mathworks.com/matlabcentral/fileexchange/11462-npermutek

Subject: Help needed - Adding up combinations of elements in a matrix.

From: Drew

Date: 17 Nov, 2009 00:07:01

Message: 6 of 7

Thank for you the responses so far. I can elaborate, sorry it was originally unclear.

If I have a 3x3 matrix,
1 2 3
4 5 6
7 8 9

The function I am trying to develop: take any value from the first row, then any value from the second row, and any value from the third row, and add them together. The guidelines are that the elements are chosen from the first row first, the second row second, and the third row third. So, no matter what, if you had (row,column) location of the three elements you picked out, the row value would be increasing by 1 (1 to 3) and the column value could be anything within 1 to 3.

So, (1,1) + (2,1) + (3,1) would be one combination. (1,1) + (2,2) + (3,3) is another possible solution. (1,3) + (2,1) + (3,2) could be another, and so forth.

For me, the easiest way to began developing an algorithm for this is writing out all possible solutions if I started from (1,1).
(1,1) (2,1) (3,1)
(1,1) (2,2) (3,2)
(1,1) (2,3) (3,3)
(1,1) (2,1) (3,3)
etc. etc.

In my head, I want the loop to pull random elements from rows 1-3, the first element from the first row, the second from the second, the third from the third row, and add them together until the sum is equal to a target integer I have previously defined. If it reaches the target, then the loop can stop.

Thanks again for your help.

Subject: Help needed - Adding up combinations of elements in a matrix.

From: Matt Fig

Date: 17 Nov, 2009 00:24:04

Message: 7 of 7

So modify the solution I gave above to get rid of F and the outer loop.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com