How to obtain sum over coalition?

3 views (last 30 days)
For a given table
1st player 2nd player score
a b 20
b c 10
b d 15
a d 10
I want to get the following results
{a}=0 {b}=0 {c}=0 {d}=0 {a b}=20 {a c}=0 {a d}=10 {b c}=10 {b d}=15 {c d}=0 {a b c}=30 {a b d}=35
{a c d}=10 {b c d} = 25 {a b c d}= 55
Is it possible to obtain above result using matlab code?
Thanks for your comment

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 29 Mar 2019
Edited: Andrei Bobrov on 29 Mar 2019
s = string(('a':'d')');
ss = s([1 ,2;2,3;2,4;1,4]);
v = [20,10,15,10]';
C = cell(4,1);
for ii = 1:4
k = num2cell(nchoosek(s,ii),2);
C{ii} = cell2table(k,'v',{'abcd'});
C{ii}.value = cellfun(@(x)sum(v(all(ismember(ss,x),2))),k);
end
or
s = string(('a':'d')');
ss = s([1 ,2;2,3;2,4;1,4]);
v = [20,10,15,10]';
C = cell(4,1);
for ii = 1:4
a = nchoosek(s,ii);
if ii == 1
b = a;
else
b = join(a,'');
end
C{ii} = array2table(b,'v',{'abcd'});
C{ii}.value = cellfun(@(x)sum(v(all(ismember(ss,x),2))),num2cell(a,2));
end
out = cat(1,C{:});
  4 Comments
HOJUNG LEE
HOJUNG LEE on 31 Mar 2019
Hello, those are samples of my data.
Thanks for all your comments, it helps me a lot.
a b 19
a b 20
c b 20
b c 21
c b 21
a b 28
b c 28
a b 30
Andrei Bobrov
Andrei Bobrov on 31 Mar 2019
No, attach your data as mat - file.

Sign in to comment.

More Answers (2)

Andrei Bobrov
Andrei Bobrov on 1 Apr 2019
Edited: Andrei Bobrov on 1 Apr 2019
Variant for your new data from example.mat (B).
BB = B{:,1:2};
abc = unique(BB(:));
n = numel(abc);
C = cell(n,1);
for ii = 1:n
D = nchoosek(abc,ii);
a = string(D);
if ii == 1
b = a;
else
b = join(a,'');
end
C{ii} = array2table(b,'v',{'abcd'});
C{ii}.value = arrayfun(@(x)sum(B.TotalRevenue(...
all(ismember(BB,D(x,:)),2))),(1:size(D,1))');
end
out = cat(1,C{:});
  1 Comment
HOJUNG LEE
HOJUNG LEE on 1 Apr 2019
Thanks for all your help! It nicely works and really helps me a lot!

Sign in to comment.


HOJUNG LEE
HOJUNG LEE on 1 Apr 2019
Here is what I'm working on.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!