How to make a 2-layer Subtotal from a table

조회 수: 5 (최근 30일)
Edson
Edson 2015년 10월 14일
댓글: Edson 2015년 10월 15일
Hi, I am trying to make a function for getting a subtotal from a Table. I attached an excel file. First sheet are inputs, second sheet is the desired out. My data consists on three columns: the first layer (financial assets codes), the second layer (fund names where the asset is located) and third column (the amount invested). Any help please... would be deeply appreciated. Regards, Edson.

채택된 답변

Peter Perkins
Peter Perkins 2015년 10월 15일
Edson, I think this does the trick:
>> question = readtable('question.xlsx')
question =
First Second Amount
________ _______ ______
'XS0001' 'Fund1' 20
'XS0002' 'Fund1' 30
'XS0003' 'Fund2' 40
'XS0004' 'Fund1' 50
'XS0005' 'Fund2' 60
'XS0006' 'Fund1' 70
'XS0001' 'Fund2' 80
'XS0002' 'Fund2' 90
'XS0003' 'Fund2' 20
'XS0004' 'Fund1' 30
'XS0005' 'Fund2' 40
'XS0006' 'Fund1' 50
'XS0001' 'Fund2' 60
'XS0002' 'Fund1' 70
'XS0003' 'Fund1' 80
'XS0004' 'Fund2' 90
'XS0005' 'Fund1' 20
'XS0006' 'Fund2' 30
'XS0001' 'Fund2' 40
'XS0002' 'Fund1' 50
'XS0003' 'Fund2' 60
'XS0004' 'Fund1' 70
'XS0005' 'Fund2' 80
'XS0006' 'Fund1' 90
>> varfun(@sum,question,'GroupingVariable',{'First' 'Second'},'InputVariable','Amount')
ans =
First Second GroupCount sum_Amount
________ _______ __________ __________
XS0001_Fund1 'XS0001' 'Fund1' 1 20
XS0001_Fund2 'XS0001' 'Fund2' 3 180
XS0002_Fund1 'XS0002' 'Fund1' 3 150
XS0002_Fund2 'XS0002' 'Fund2' 1 90
XS0003_Fund1 'XS0003' 'Fund1' 1 80
XS0003_Fund2 'XS0003' 'Fund2' 3 120
XS0004_Fund1 'XS0004' 'Fund1' 3 150
XS0004_Fund2 'XS0004' 'Fund2' 1 90
XS0005_Fund1 'XS0005' 'Fund1' 1 20
XS0005_Fund2 'XS0005' 'Fund2' 3 180
XS0006_Fund1 'XS0006' 'Fund1' 3 210
XS0006_Fund2 'XS0006' 'Fund2' 1 30
You might also consider converting First and Second to categorical, something like
question.First = categorical(question.First);
Hope this helps.
  댓글 수: 1
Edson
Edson 2015년 10월 15일
Thanks a lot Peter! Works like a charm! That solves my question entirely.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Edson
Edson 2015년 10월 15일
Thanks a lot Peter! Works like a charm! That solves my question entirely.

카테고리

Help CenterFile Exchange에서 Relational Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by