MATLAB Answers

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
the cyclist
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!