Pivoting a Table and Computing the Weighted Mean

5 visualizaciones (últimos 30 días)
Robert Garrison
Robert Garrison el 27 de Abr. de 2016

I have the following table imported into MATLAB:

>> stocks=readtable(test_set,'Format','%{yyyymmdd}D %s %f %f %s')

stocks =

      Date      Ticker    Price    Volume    Industry 
    ________    ______    _____    ______    _________
    20160404    'BANK'    75.56    445       'Finance'
    20160404    'BOND'    52.81    590       'Finance'
    20160404    'GAS'     37.35    389       'Energy' 
    20160405    'BOND'    60.67    534       'Finance'
    20160405    'GAS'     42.32    510       'Energy' 
    20160405    'OIL'     48.69    577       'Energy' 
    20160406    'BANK'     80.4    441       'Finance'
    20160406    'BANK'    81.06    573       'Finance'
    20160406    'OIL'     55.59    639       'Energy' 
    20160408    'BOND'    61.74    499       'Finance'
    20160408    'GAS'     43.26    527       'Energy' 
    20160408    'OIL'     56.53    473       'Energy' 
    20160409    'BANK'    79.48    508       'Finance'
    20160409    'GAS'     38.39    487       'Energy' 
    20160409    'OIL'     54.28    553       'Energy' 
    20160410    'BOND'    59.18    431       'Finance'
    20160410    'GAS'     38.59    612       'Energy' 
    20160410    'OIL'      61.4    516       'Energy' 
    20160411    'BANK'    84.48    577       'Finance'
    20160411    'BOND'     67.3    399       'Finance'
    20160411    'OIL'     51.34    400       'Energy' 
    20160412    'BANK'    77.42    590       'Finance'
    20160412    'BOND'    55.83    442       'Finance'
    20160412    'GAS'     37.43    436       'Energy'

I want to "pivot" the table to have the dates on the rows and the industry as the column headers. The resulting table should be 9 x 3 (including the header row):

Date        Energy    Finance
20160404     
20160405
20160406
20160408
20160409
20160410
20160411
20160412

With the values being the average price weighted by volume. I attempted to the use the "splitapply" and "findgroups" functionality with limited success. Any assistance would be greatly appreciated.

Respuestas (0)

Categorías

Más información sobre Financial Toolbox en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by