Sum if multiple conditions satisfied across vectors

2 次查看(过去 30 天)
Hi all,
I have three vector that represent in which market a product is, to which group of product it belongs within the market, and the share of market. Let's say I have 2 markets with 3 products in each market and 2 group of products per market:
MKT = [1 ; 1 ; 1 ; 2 ; 2 ; 2]
GROUP = [1 ; 1 ; 2 ; 1 ; 2 ; 2]
SHARE = [0.2 ; 0.3 ; 0.5 ; 0.6 ; 0.1 ; 0.3]
I want to create a new vector that tells me for each product the share of the group to which it belongs within its market. The result should be:
SHARE_GROUP = [0.5 ; 0.5 ; 0.5 ; 0.6 ; 0.4 ; 0.4]
I've tried using accumarray but wasn't able to solve this.
Thanks

采纳的回答

Andrei Bobrov
Andrei Bobrov 2019-9-2
编辑:Andrei Bobrov 2019-9-2
MKT = [1 ; 1 ; 1 ; 2 ; 2 ; 2];
GROUP = [1 ; 1 ; 2 ; 1 ; 2 ; 2];
SHARE = [0.2 ; 0.3 ; 0.5 ; 0.6 ; 0.1 ; 0.3];
T = table(MKT,GROUP,SHARE);
Share_group = varfun(@sum,T,'GroupingVariables',{'MKT','GROUP'});
[~,ii] = ismember(T(:,{'MKT','GROUP'}),Share_group(:,{'MKT','GROUP'}),'rows');
T.SHARE_GROUP = Share_group.sum_SHARE(ii);
or
[~,~,c] = unique([MKT,GROUP],'rows');
group_share = accumarray(c,SHARE,[],@sum);
SHARE_GROUP = group_share(c);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Gamma Functions 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by