MATLAB: Compare and calculate two excel sheet

compare

I have two excel sheet, one contains the social security no. and gender, the other one contains social security no. and each person's income, now I have to calculate the average income for each gender. I understand that I need to use the total income of each gender to divide the total number of them, as they are in two excel sheet, I m lost. Any one can help? thank you so much

Best Answer

Hello Huitian Mao,
I hope this will be helpful. I've attached a sample excel sheet of gender and income too. Change the location of excel sheets and run the code. It works as per your requirement.
%Assumptions:
%Both excel sheets have same number of rows
%1st coloum is social serial number and 2nd coloum is gender or income in
%both the sheets
clc;
[num,txt,raw] = xlsread('C:\Rahul\gender.xls');
[num1,txt1,raw1] = xlsread('C:\Rahul\income.xls');
num1=num1(:,2); %income
MaleIncome=0;
NumberOfMales=0;
MaleAvgIncome=0;
FemaleIncome=0;
NumberOfFemales=0;
FemaleAvgIncome=0;
for i=1:10
if char(txt(i))=='M'
MaleIncome=MaleIncome+num1(i);
NumberOfMales=NumberOfMales+1;
elseif char(txt(i))=='F'
FemaleIncome=FemaleIncome+num1(i);
NumberOfFemales=NumberOfFemales+1;
end
end
MaleAvgIncome=MaleIncome/NumberOfMales;
disp('Male average income : ')
disp(MaleAvgIncome);
FemaleAvgIncome=FemaleIncome/NumberOfFemales;
disp('Female average income : ')
disp(FemaleAvgIncome);