Create a function - with cross reference

4 次查看(过去 30 天)
Hello,
I hope you are able to help me. Im fairly new to Matlab and need some help.
My question is that I have 3 excel sheets that I have imported through xlsread. In 1 document, I have Income (social security number that correlates with income), document 2, I have citizens (Social security number that correlates to name, gender and which region they live in), and the final document I have Taxes (region that correlates to amount of tax you pay)
My issue is that now I have to calculate the paid tax in each region and I dont know how to do so.
I have no idea of what to do, besides that I need to get the region, correlate it to the social security number to finally get the income. Can someone please help me?
Thank you in advance, - Emil

回答(1 个)

Guillaume
Guillaume 2017-5-28
Your question is a bit thin on details (like what format are the inputs in, what it is exactly you want to calculate, etc.). The following should give you an idea of how to do whatever it is you want. Adapt as required:
%demo inputs:
incomes = table((1000:1010)', randi(10, 11, 1)*1e4, 'VariableNames', {'ssn', 'income'})
persons = table((1000:1010)', char(randi(double('az'), 11, 5)), categorical(randi([0 1], 11, 1), [0 1], {'Male', 'Female'}), randi(5, 11, 1), 'VariableNames', {'ssn', 'name', 'gender', 'region'})
taxes = table((1:5)', randi([10 20], 5, 1)/100, 'VariableNames', {'region', 'tax'})
%join tables:
alltaxes = join(join(incomes, persons), taxes)
%sum of income * tax, per region
taxperregion = rowfun(@(income, tax) sum(income .* tax), alltaxes, 'InputVariables', {'income', 'tax'}, 'GroupingVariables', 'region', 'OutputVariableNames', 'totaltax')

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by