MATLAB: Comparing two values of a matrice

MATLABmatrix

The two columns: Sex(0 = female, 1 = male) Chest Pains(1 = low , 2 = normal , 3 = abnormal, 4 = high)
I tried to compare them using a histogram
hist(age,sex)
I get an error saying that I need unique xValues. I think this is due to Sex having the value 1 to represent male and also 1 represents chest pains for my other column.
Is there another way to compare two columns? My end goal is to see the males min, max and average chest pains (same for female).

Best Answer

Ok, I'm a bit unclear why we have a variable for age but not one for chest pain in your question. Never mind, let's assume it's called chestpain.
The easiest is to first put all this information into a table, which also gives you prettier display:
patients = table(age(:), sex(:), chestpain(:), 'VariableNames', {'age', 'sex', 'chestpain'})
For even more prettiness, you could change the sex variable to a categorical array:
patients.sex = categorical(patients.sex, [0 1], {'F', 'M'})
Whether or not you do, does not matter for what follows up.
To calculate the min, max and average chest pain of a group, let's create a separate function for that. In its own chestpain_stats.m file:
function [minpain, maxpain, meanpain] = chestpain_stats(pain)
%pain: a column vector
minpain = min(pain);
maxpain = max(pain);
meanpain = mean(pain);
end
After that, calculating the stats per sex is simply:
patientstats = rowfun(@chestpain_stats, patients, 'GroupingVariable', 'sex', 'InputVariables', 'chestpain', 'OutputVariableNames', {'minpain', 'maxpain', 'meanpain'})
See rowfun, varfun and splitapply for more ways to compute stats on a table.