Splitting data array into sub arrays

6 views (last 30 days)
Francis Chabot on 19 Jan 2021
Commented: Francis Chabot on 4 Feb 2021
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.
Regards,
Frank
dpb on 19 Jan 2021
Would need more than just one sample number to know the format...if it's always the first two digits of a variable-length number/string, then converting to character and extracting the first two characters is probably as simple a solution as any.
If it's always a four-digit number like the example, then
mod(v,100)
would work, but wouldn't if were a five- or three-digit number.
Need a complete definition of the input patterns possible.
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

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.
dpb on 3 Feb 2021
Edited: dpb on 3 Feb 2021
I showed you how to do that -- use fix(SIC/100) as the grouping variable.
However, that said, I see that I forgot to include that in the previous code and just used SIC.
The way things work in identifying the grouping variable, you can't pass a calculation in rowfun so you need to create the variable for the purpose.
>> tSIC.Industry=fix(tSIC.SIC/100); % define industry code for grouping
>> tSIC=tSIC(:,[1 end 2]); % rearrange table for convenience
>> tSIC % show resulting table
tSIC =
2×3 table
SIC Industry Data
____ ________ ____________________
3301 33 12 32 21 92
4502 45 32 45 32 65
>> rowfun(@median,tSIC,'GroupingVariables','Industry', ...
'InputVariables','Data', ...
'OutputVariableNames','Median')
ans =
2×3 table
Industry GroupCount Median
________ __________ ______
33 1 26.5
45 1 38.5
>>
As noted above, will need to modify to handle multiple cases of the same ID by using the anonymous function and the 'all' parameter to compute overall group median.
Francis Chabot on 4 Feb 2021
Awesome!
I forgot change the 'SIC' to 'Industry' but finally figured it out.
Best regards,
Frank

Categories

Find more on Logical in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!