How to plot a matrix with several variables?
4 views (last 30 days)
Show older comments
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
Answers (1)
Star Strider
on 7 May 2016
Edited: Star Strider
on 7 May 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))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/174256/image.png)
EDIT — Added plot figure. Code unchanged.
2 Comments
Star Strider
on 7 May 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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!