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:
Summing content of one matrix based on another

Subject: Summing content of one matrix based on another

From: Ulrik Nash

Date: 7 Jul, 2011 13:27:10

Message: 1 of 8

Hi Everyone,

Suppose I have matrix A

    51 53 69 64 50 56
    69 79 43 68 58 51
    60 73 58 57 46 66
    62 52 54 61 58 56

and matrix B

     1 2 3 5 4 6
     4 1 5 2 3 6
     4 2 5 3 1 6
     2 1 3 4 5 6

now I wish to fin C, which is a vector that contains sums of values in A, based on values in B. More precisely, the first value in C is equal to the sum of values in A that have a corresponding 1 in matrix B. The second value in C is equal to the sum of values in A that have a corresponding 2 in matrix B, and so on until 6, which is the number of columns of A and B.

The answer in this case is

228 256 238 240 223 229

How can this be done?

Regards,

Ulrik.

Subject: Summing content of one matrix based on another

From: Greg von Winckel

Date: 7 Jul, 2011 14:06:08

Message: 2 of 8

Does this do what you want?

cell2mat(arrayfun(@(k) sum(A(B==k)),1:6,'UniformOutput',false))

might not be very efficient if A and B are large.

Subject: Summing content of one matrix based on another

From: Roger Stafford

Date: 7 Jul, 2011 19:16:09

Message: 3 of 8

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <iv4c7e$gv5$1@newscl01ah.mathworks.com>...
> Hi Everyone,
>
> Suppose I have matrix A
>
> 51 53 69 64 50 56
> 69 79 43 68 58 51
> 60 73 58 57 46 66
> 62 52 54 61 58 56
>
> and matrix B
>
> 1 2 3 5 4 6
> 4 1 5 2 3 6
> 4 2 5 3 1 6
> 2 1 3 4 5 6
>
> now I wish to fin C, which is a vector that contains sums of values in A, based on values in B. More precisely, the first value in C is equal to the sum of values in A that have a corresponding 1 in matrix B. The second value in C is equal to the sum of values in A that have a corresponding 2 in matrix B, and so on until 6, which is the number of columns of A and B.
>
> The answer in this case is
>
> 228 256 238 240 223 229
>
> How can this be done?
>
> Regards,
>
> Ulrik.
- - - - - - - - - - -
 [m,n[ = size(A);
 C = sum(reshape(A(sub2ind([m,n],repmat((1:m).',n,1),B(:))),m,n),1);

Roger Stafford

Subject: Summing content of one matrix based on another

From: Roger Stafford

Date: 7 Jul, 2011 22:30:25

Message: 4 of 8

"Roger Stafford" wrote in message <iv50lp$ml3$1@newscl01ah.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <iv4c7e$gv5$1@newscl01ah.mathworks.com>...
> > The answer in this case is
> > 228 256 238 240 223 229
> [m,n[ = size(A);
> C = sum(reshape(A(sub2ind([m,n],repmat((1:m).',n,1),B(:))),m,n),1);
- - - - - - - - - -
  My result for your example agrees only with the first and the last elements of C. The middle four are different. Did you make a mistake in addition or do I not understand your problem?

  The first line is supposed to be:

 [m,n] = size(A);

Roger Stafford

Subject: Summing content of one matrix based on another

From: Bruno Luong

Date: 8 Jul, 2011 05:25:10

Message: 5 of 8

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <iv4c7e$gv5$1@newscl01ah.mathworks.com>...
> Hi Everyone,
>
> Suppose I have matrix A
>
> 51 53 69 64 50 56
> 69 79 43 68 58 51
> 60 73 58 57 46 66
> 62 52 54 61 58 56
>
> and matrix B
>
> 1 2 3 5 4 6
> 4 1 5 2 3 6
> 4 2 5 3 1 6
> 2 1 3 4 5 6
>
> now I wish to fin C, which is a vector that contains sums of values in A, based on values in B. More precisely, the first value in C is equal to the sum of values in A that have a corresponding 1 in matrix B. The second value in C is equal to the sum of values in A that have a corresponding 2 in matrix B, and so on until 6, which is the number of columns of A and B.
>
> The answer in this case is
>
> 228 256 238 240 223 229
>
> How can this be done?
>

By a single command ACCUMARRAY:

>> A = [51 53 69 64 50 56
    69 79 43 68 58 51
    60 73 58 57 46 66
    62 52 54 61 58 56 ]

B = [1 2 3 5 4 6
     4 1 5 2 3 6
     4 2 5 3 1 6
     2 1 3 4 5 6]

>> accumarray(B(:),A(:))

ans =

   228
   256
   238
   240
   223
   229

% Bruno

Subject: Summing content of one matrix based on another

From: Roger Stafford

Date: 8 Jul, 2011 07:18:09

Message: 6 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <iv64bm$m9o$1@newscl01ah.mathworks.com>...
> >> accumarray(B(:),A(:))
> ans =
> 228
> 256
> 238
> 240
> 223
> 229
- - - - - - - - - -
  It appears I didn't read the problem carefully enough. It was that first sum (51+68+57+52) that correctly equaled 228 with my interpretation that threw me off. Your interpretation is undoubtedly the correct one, Bruno.

Roger Stafford

Subject: Summing content of one matrix based on another

From: Rune Allnor

Date: 8 Jul, 2011 07:59:48

Message: 7 of 8

On Jul 7, 3:27 pm, "Ulrik Nash" <u...@sam.sdu.dk> wrote:
> Hi Everyone,
...
> How can this be done?

You have already recieved some answers that produce
some result, which may or may not be correct. Even
if correct, the responses I have seen so far are
likely not very helpful to you, as they do not
attempt o explain how to solve the problem but
rather tend to show off the respionder's intimacy
with obscure matlab.

To walk you through this, define the matrices A and B:

>> A=[51 53 69 64 50 56
    69 79 43 68 58 51
    60 73 58 57 46 66
    62 52 54 61 58 56 ];
>> B = [ 1 2 3 5 4 6
     4 1 5 2 3 6
     4 2 5 3 1 6
     2 1 3 4 5 6 ];

Next, find the sequence of unique numbers in B:

>> ilist = unique(B(:));

Now you know how many elements you will need to
store in the resulting array C:

>> C = zeros(size(ilist));

Next, loop through the sequence ilist, find all
their positions in B, and sum the corresponding
numbers in A:

>> for n=1:length(ilist)
   idx = find(B(:) == ilist(n));
   C(n) = sum(A(idx));
   end

After which C contains the desired result.

As you can see from some of the other responses,
using too obscure commands tends to confuse the
programmer so that, while the program looks 'cool'
or 'impressive' or whatever, it might be plain wrong.

Writing simple, understandable code the first time
around tends to redice coder confusion and thus, the
number of coding errors.

Rune

Subject: Summing content of one matrix based on another

From: Ulrik Nash

Date: 8 Jul, 2011 09:28:07

Message: 8 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <iv64bm$m9o$1@newscl01ah.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <iv4c7e$gv5$1@newscl01ah.mathworks.com>...
> > Hi Everyone,
> >
> > Suppose I have matrix A
> >
> > 51 53 69 64 50 56
> > 69 79 43 68 58 51
> > 60 73 58 57 46 66
> > 62 52 54 61 58 56
> >
> > and matrix B
> >
> > 1 2 3 5 4 6
> > 4 1 5 2 3 6
> > 4 2 5 3 1 6
> > 2 1 3 4 5 6
> >
> > now I wish to fin C, which is a vector that contains sums of values in A, based on values in B. More precisely, the first value in C is equal to the sum of values in A that have a corresponding 1 in matrix B. The second value in C is equal to the sum of values in A that have a corresponding 2 in matrix B, and so on until 6, which is the number of columns of A and B.
> >
> > The answer in this case is
> >
> > 228 256 238 240 223 229
> >
> > How can this be done?
> >
>
> By a single command ACCUMARRAY:
>
> >> A = [51 53 69 64 50 56
> 69 79 43 68 58 51
> 60 73 58 57 46 66
> 62 52 54 61 58 56 ]
>
> B = [1 2 3 5 4 6
> 4 1 5 2 3 6
> 4 2 5 3 1 6
> 2 1 3 4 5 6]
>
> >> accumarray(B(:),A(:))
>
> ans =
>
> 228
> 256
> 238
> 240
> 223
> 229
>
> % Bruno

Thank you all those who helped out. Bruno's simple solution gets the vote, so thank you Bruno! Kind regards, Ulrik.

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