Splitting data array into sub arrays

3 views (last 30 days)
Hi, I want to split the data of an array by the first 2 numbers of each data. This is to split firms by the first 2 digit of their SIC codes.
Example: 3301 would be in the sub array 33 which would represent a sector.
Any suggestions would be helpful.
Regards,
Frank
  2 Comments
Francis Chabot
Francis Chabot on 20 Jan 2021
Thanks for the help.
The format are four digits numbers that needs to be classified by the first two numbers.
Example : 3301, 4502, 3306, 4602, 4510...
Both numbers that starts with 33 and 45 would be classified into the same sub array so I would then be able to easily compute the median of each sub array.
Speaking of scale there's about 100 sub array possible on about 20 000 firms.
Regards,
Frank

Sign in to comment.

Accepted Answer

dpb
dpb on 20 Jan 2021
>> SIC=[3301, 4502, 3306, 4602, 4510].';
>> splitapply(@median,SIC,findgroups(fix(SIC/100)))
ans =
3303.50
4506.00
4602.00
>>
NB: The use of mod above was in error, dunno how I came up with that, but as long as they're all four-digit codes, the above should be about as easy as it gets.
  7 Comments
Francis Chabot
Francis Chabot on 4 Feb 2021
Awesome!
I forgot change the 'SIC' to 'Industry' but finally figured it out.
Best regards,
Frank

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!