How to sort a table into five subsets?

Hi,
I have table with stocks. Now I would like to sort them into subsets according to their size. My goal is to create the 5 subsets according to their size, with each of them holding 20% of the stocks. Unfortunatley I don't know how to do it. The size is the market capitalization. The dataset is attached to the question.
ID is stands for the diffrent stocks
years are the oberved years (1962-2016)
sumMC is the market capitalization of each stock in every year.
Does someone know how to do it?
Thank you in advance

4 件のコメント

Walter Roberson
Walter Roberson 2021 年 4 月 25 日
What is the way you decide which stock goes into which subset?
Luca
Luca 2021 年 4 月 25 日
I want to sort them according the market capitalisation. So one subset holds the 20% biggest stocks. The second 21%-40% biggest stocks and so on
Image Analyst
Image Analyst 2021 年 4 月 25 日
So what was wrong with the code I posted below? I have about 1/5 of all companies in each groups. Did you want 1/5 or the dollar amount (instead of 1/5 the number of companies) in each group? Please be clear.
Luca
Luca 2021 年 4 月 25 日
Sorry I didn't get anround to test it as I was answering his question. It works perfectly thank you very much.

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2021 年 4 月 25 日

0 投票

Just use sort and linear indexing
sortedCaps = sort(marketCap, 'Ascend');
stocksPerGroup = length(sortedCaps) / 5;
% Extract stocks into group #1.
firstIndex = 1;
lastIndex = round(stocksPerGroup);
group1 = sortedCaps(firstIndex : lastIndex);
% Extract stocks into group #2.
firstIndex = lastIndex + 1;
lastIndex = round(2 * stocksPerGroup);
group2 = sortedCaps(firstIndex : lastIndex);
% Extract stocks into group #3.
firstIndex = lastIndex + 1;
lastIndex = round(3 * stocksPerGroup);
group3 = sortedCaps(firstIndex : lastIndex);
% Extract stocks into group #4.
firstIndex = lastIndex + 1;
lastIndex = round(4 * stocksPerGroup);
group4 = sortedCaps(firstIndex : lastIndex);
% Extract stocks into group #5.
firstIndex = lastIndex + 1;
lastIndex = length(sortedCaps);
group5 = sortedCaps(firstIndex : lastIndex);

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品

リリース

R2021a

質問済み:

2021 年 4 月 25 日

コメント済み:

2021 年 4 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by