How to plot a matrix with several variables?
古いコメントを表示
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
1 件のコメント
Walter Roberson
2016 年 6 月 21 日
編集済み: Walter Roberson
2016 年 6 月 21 日
回答 (1 件)
Star Strider
2016 年 5 月 7 日
編集済み: Star Strider
2016 年 5 月 7 日
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 件のコメント
Matrix-Matlab
2016 年 5 月 7 日
Star Strider
2016 年 5 月 7 日
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.
カテゴリ
ヘルプ センター および File Exchange で Annotations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!