When inputting an x value how to output a y

1 visualización (últimos 30 días)
Ashley Megow
Ashley Megow el 30 de Oct. de 2020
Comentada: Ameer Hamza el 30 de Oct. de 2020
using this table I want to input a YEAR as the x, and want to output both the male and female as y values in the command window.

Respuestas (1)

Ameer Hamza
Ameer Hamza el 30 de Oct. de 2020
Editada: Ameer Hamza el 30 de Oct. de 2020
One approach is to use interp1()
years; % all year values
male_vals; % male values
female_vals; % female values
vals = [male_vals female_vals];
mdl = @(year) interp1(years, vals, year)
You can then use mdl() as a function.
mdl(2000); % it will give values for year 2000
Another approach is to just use indexing
years; % all year values
male_vals; % male values
female_vals; % female values
vals = [male_vals female_vals];
mdl = @(year) vals(years==year, :);
You can also call it like a function as shown above.
  4 comentarios
Ashley Megow
Ashley Megow el 30 de Oct. de 2020
I figured it would just be easier to send pictures of my code
Ameer Hamza
Ameer Hamza el 30 de Oct. de 2020
The elseif block will go like this
elseif (yearWant==1990)
vals = [tbl.Male(:) tbl.Female(:)];
valueWant = interp1(tbl.Year(:), vals, yearWant)
end

Iniciar sesión para comentar.

Categorías

Más información sobre Tables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by