Splitting data array into sub arrays
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2021-1-19
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.
采纳的回答
dpb
2021-1-20
>> 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 个评论
dpb
2021-2-3
编辑:dpb
2021-2-3
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Events 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!