How to obtain sum over coalition?

조회 수: 3 (최근 30일)
HOJUNG LEE
HOJUNG LEE 2019년 3월 29일
댓글: HOJUNG LEE 2019년 4월 1일
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