필터 지우기
필터 지우기

How to plot a matrix with several variables?

조회 수: 6 (최근 30일)
Matrix-Matlab
Matrix-Matlab 2016년 5월 7일
편집: Walter Roberson 2016년 6월 21일
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개)

Star Strider
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
Matrix-Matlab 2016년 5월 7일
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 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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by