How to obtain sum over coalition?

2 次查看(过去 30 天)
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

采纳的回答

Andrei Bobrov
Andrei Bobrov 2019-3-29
编辑:Andrei Bobrov 2019-3-29
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 个评论
HOJUNG LEE
HOJUNG LEE 2019-3-31
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 2019-3-31
No, attach your data as mat - file.

请先登录,再进行评论。

更多回答(2 个)

Andrei Bobrov
Andrei Bobrov 2019-4-1
编辑:Andrei Bobrov 2019-4-1
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 个评论
HOJUNG LEE
HOJUNG LEE 2019-4-1
Thanks for all your help! It nicely works and really helps me a lot!

请先登录,再进行评论。


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

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by