필터 지우기
필터 지우기

When inputting an x value how to output a y

조회 수: 1 (최근 30일)
Ashley Megow
Ashley Megow 2020년 10월 30일
댓글: Ameer Hamza 2020년 10월 30일
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.

답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 10월 30일
편집: Ameer Hamza 2020년 10월 30일
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
Ashley Megow
Ashley Megow 2020년 10월 30일
I figured it would just be easier to send pictures of my code
Ameer Hamza
Ameer Hamza 2020년 10월 30일
The elseif block will go like this
elseif (yearWant==1990)
vals = [tbl.Male(:) tbl.Female(:)];
valueWant = interp1(tbl.Year(:), vals, yearWant)
end

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by