How to obtain sum over coalition?

3 vues (au cours des 30 derniers jours)
HOJUNG LEE
HOJUNG LEE le 29 Mar 2019
Commenté : HOJUNG LEE le 1 Avr 2019
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

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 29 Mar 2019
Modifié(e) : Andrei Bobrov le 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 commentaires
HOJUNG LEE
HOJUNG LEE le 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 le 31 Mar 2019
No, attach your data as mat - file.

Connectez-vous pour commenter.

Plus de réponses (2)

Andrei Bobrov
Andrei Bobrov le 1 Avr 2019
Modifié(e) : Andrei Bobrov le 1 Avr 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 commentaire
HOJUNG LEE
HOJUNG LEE le 1 Avr 2019
Thanks for all your help! It nicely works and really helps me a lot!

Connectez-vous pour commenter.


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

Catégories

En savoir plus sur Time Series Objects dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by