Defining a fitline only up to a specific x-tick

조회 수: 1 (최근 30일)
Sophie Blankenheim
Sophie Blankenheim 2021년 6월 16일
편집: the cyclist 2021년 6월 16일
I am trying to plot some age specific data and only want to plot a fitline through a specific age (19 years). I need all of the data to be plotted as a scatterplot but only want a fitline from data younger than 19.
This is what I have right now and it makes a fitline across the entire dataset
scatter(CarsCasesFemale1(:,1),CarsCasesFemale1(:,2),'r','o')%
scatter(CarsCasesMale1(:,1),CarsCasesMale1(:,2),'b','s')%
plot(femaleCases1(:,1), f_CarsF,'-r')
plot(maleCases1(:,1), f_CarsM,'-b')

채택된 답변

the cyclist
the cyclist 2021년 6월 16일
편집: the cyclist 2021년 6월 16일
It should be straightforward, but it is not possible to deduce from your code how to do it. In particular, since none of your variables mention age, and there is no annotation mentioning it, we can't figure out how to apply the restriction you want.
Can you upload the data needed to run the code, and identify the age?
That being said, if CarsCasesFemale1(:,1) is the age variable, which seems possible, then I think this does what you want.
isFemaleUnder19 = femaleCases1(:,1) < 19;
plot(femaleCases1(isFemaleUnder19,1), f_CarsF(isFemaleUnder19),'-r')
and analogous code for males.
Even if it doesn't do what you want, maybe you can see the logic, and figure it out.
  댓글 수: 2
Sophie Blankenheim
Sophie Blankenheim 2021년 6월 16일
Here is the definition of the variables that includes age. I can't upload the entire dataset but the data is split up into the columns with the variables that I am looking at, age, and sex.
femaleCasesIndex1 = (SexColumn == 1);
femaleCases1 = [AgeColumn(femaleCasesIndex1), DataColumn(femaleCasesIndex1)];
CarsCasesFemaleIndex1 = ( SexColumn == 1 & WordColumn == 1);
CarsCasesFemale1 = [AgeColumn(CarsCasesFemaleIndex1), DataColumn(CarsCasesFemaleIndex1)];
...
maleCasesIndex1 = (SexColumn == 2);
maleCases1 = [AgeColumn(maleCasesIndex1), DataColumn(maleCasesIndex1)];
CarsCasesMaleIndex1 = ( SexColumn == 2 & WordColumn == 1);
CarsCasesMale1 = [AgeColumn(CarsCasesMaleIndex1), DataColumn(CarsCasesMaleIndex1)];
the cyclist
the cyclist 2021년 6월 16일
편집: the cyclist 2021년 6월 16일
Did you try my code? Because it seems like the logic should work, if I've understood it:
femaleCases1 is an Nx2 array in which the first column is the age of the females, right?
Therefore, my variable isFemaleUnder19 is a logical variable which is true when the female is strictly under 19, and false otherwise.
Therefore, femaleCases1(isFemaleUnder19,1) is the ages of the under-19 females, and f_CarsF(isFemaleUnder19) is the fit value for them.
Therefore, the plotting code I posted plots the ages vs. the fit for the under-19 females.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by