When inputting an x value how to output a y
1 回表示 (過去 30 日間)
古いコメントを表示
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/396864/image.jpeg)
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.
0 件のコメント
回答 (1 件)
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 件のコメント
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 Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!