Defining a fitline only up to a specific x-tick

1 view (last 30 days)
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')

Accepted Answer

the cyclist
the cyclist on 16 Jun 2021
Edited: the cyclist on 16 Jun 2021
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 Comments
Sophie Blankenheim
Sophie Blankenheim on 16 Jun 2021
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 on 16 Jun 2021
Edited: the cyclist on 16 Jun 2021
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.

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!