Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Summing content of one matrix based on another
Date: Thu, 7 Jul 2011 19:16:09 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 33
Message-ID: <iv50lp$ml3$1@newscl01ah.mathworks.com>
References: <iv4c7e$gv5$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-01-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1310066169 23203 172.30.248.46 (7 Jul 2011 19:16:09 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 7 Jul 2011 19:16:09 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:735423

"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