Effacer les filtres
Effacer les filtres

How to plot a matrix with several variables?

6 vues (au cours des 30 derniers jours)
Matrix-Matlab
Matrix-Matlab le 7 Mai 2016
Hello,
I have this question that I dont know how to solve.
Generate the matrix “Customers” containing random values for the following variables. The matrix must contain 3000 people.
a. Gender. Must be either 1 or 0. 0 signifies that the person is female.
b. Age. Must be between 21 and 85.
c. Insurance risk. Must be a value between 1 and 10.
The above I have solved but the following question I have trouble solving:
"Create a plot that shows the distribution of the insurance risk for males"
Can anyone help me solve this please?
Thank you in advance
- Emil

Réponses (1)

Star Strider
Star Strider le 7 Mai 2016
Modifié(e) : Star Strider le 7 Mai 2016
Building on Andrei’s code:
Customers.Gender = randi([0,1],3000,1);
Customers.Age = randi([21,85],3000,1);
Customers.Insurance_risk = randi(10,3000,1);
RiskMale = Customers.Insurance_risk(Customers.Gender == 1);
[DistRiskMale,Edges] = histcounts(RiskMale, 10); % Generate Histogram Counts
Centres = cumsum(diff(Edges)) - mean(diff(Edges))/2; % Calculate Centres For Plot
ProbRiskMale = DistRiskMale/sum(DistRiskMale); % Calculate Probability
figure(1)
bar(Centres, ProbRiskMale)
grid
xlabel('Risk Level')
ylabel('Probability')
title('Risk Probability For Males')
set(gca, 'XTick',Centres, 'XTickLabel',(1:10))
EDIT — Added plot figure. Code unchanged.
  2 commentaires
Matrix-Matlab
Matrix-Matlab le 7 Mai 2016
I dont really understand how to plot these. I have already calculated the gender, age and insurance_risk. Then the riskmale is that I take the customer insurance and bases it off of gender =1 since that is male. But wouldn't I then from there plot RiskMale?
Star Strider
Star Strider le 7 Mai 2016
You are asked to plot the distribution, that I take to mean either the number or probability in each risk category.
You can either plot ‘DistRiskMale’ for the number in each risk category or ‘ProbRiskMale’ for the probability in each risk category. To plot ‘DistRiskMale’, you need to change the bar call to:
bar(Centres, DistRiskMale)
The rest of my code remains unchanged.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by