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
0 个评论
采纳的回答
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 个评论
更多回答(2 个)
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{:});
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!