Hello,
I have a code that helps me calculate the median by SIC code that I'd like to modify to be able to use it as a function.
SICcode = xlsread('G-Score','Active','C3:C11');
ROA = xlsread('G-Score','Active','D3:D11');
SIC = [SICcode(:), ROA(:)];
splitapply(@median,SIC(:,2:end),findgroups(SIC(:,1)/100));
median(SIC(1,2:end));
tSIC=table(SIC(:,1),SIC(:,2:end),'VariableNames',{'SIC','Data'})
tSIC.Industry = fix(tSIC.SIC/100);
tSIC = tSIC(:,[1 end 2]);
rowfun(@(v)median(v,'all'),tSIC,'GroupingVariables','Industry', ...
'InputVariables','Data', ...
'OutputVariableNames','Median')
So, with this code I am able to compute the median of an industry by the first 2 digit of the SIC code.
The things is I'd like to make a function that will helps me with two things:
  1. Compute the ROA for more than one year, in this case 23 in total.
  2. Create a function that will let me do the same thing for differents statistics (ROE, Total Assets, CFO...), in this case about 20 in total.
I know it doesn't need "a lot" of work to create a function with this code but at this point I can't make it works.
Any helps would be appreciated.
Best regards,
Frank

댓글 수: 3

dpb
dpb 2021년 2월 9일
Why are you doing the same thing twice't? Either use an array or use a table; no point in doing both.
Same thing we talked before; don't read the file twice't, either; particularly with xlsread that is deprecated and quite slow.
First, you have to decide just what it is that the function is supposed to do and to take as its input -- is it a filename of the data file to be analyzed or just the variable/table to compute the desired statistic from?
Either way, the basic thing to do is to assign the result of the grouping calculation to a variable; in our previous thread I simply let it echo back to the command line to illustrate the code worked...you have to decide what you want/need as the result depending upon what is to be done next.
I did it differently this time.
Data = xlsread('G-Score','All Data');
ROA = Data(2788:5571,2:end);
SICcode = xlsread('G-Score','All','C2:C2785');
SICROA = [SICcode(:), ROA(:,1:end)];
splitapply(@nanmedian,SICROA(2:end,:),findgroups(SICROA(:,1)/100));
It's only when I come to use the splitapply that I can't due to these errors:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in splitapply>localapply (line 253)
finalOut{curVar} = vertcat(funOut{:,curVar});
Error in splitapply (line 132)
varargout = localapply(fun,splitData,gdim,nargout);
I know that it is possible because I can compute the median of each lines by doing it that way:
nanmedian(SICROA(2:end,2));
I find weird that it works by doing it separatly but when using it with the splitapply it won't work.
Best regards,
Frank
dpb
dpb 2021년 2월 10일
It's the issue I told you about before that there will be different numbers of elements in the groupings if there aren't the same number of elements per group -- and so, since median works by colum, it may also have a different number of results by group.
That's why I told you you needed to use the anonyomous function in order to pass the 'all' parameter so it returns only the overall median of the group each call.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2021년 2월 10일

0 개 추천

I think you can do what you want more simply with groupsummary.

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2021년 2월 9일

댓글:

dpb
2021년 2월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by