Part 8. Problem 71 - Problem 80.
前几天把暑假剩的两道题过了,把cody challenge做完了。最近快放国庆了,又有大段空闲时间了,白天学学算法,晚上把这个坑填了...
后面这些代码都重写了。
Problem 71. Read a column of numbers and interpolate missing data
% Given an input cell array of strings s, pick out the second column and turn it into a row vector of data. Missing data will be indicated by the number 9999. If you encounter missing data, you should perform linear interpolation to the nearest accurate data points (missing data will not occur in the first or last element).
%
% The first row is always descriptive text. So if the input cell array s is
%
% s = { ...
% 'Day Temp'
% ' 1 -5'
% ' 2 19'
% ' 3 1'
% ' 4 9999'
% ' 5 3'};
%
% then the output variable t is the following row vector.
%
% t = [-5 19 1 2 3];
%
% Here's an example of real-world data.
function ans = read_and_interp(s)
s = str2num(char(s(2:end)));
id = (s(:,2) ~= 9999);
interp1(s(id,1), s(id,2), s(:,1))';
end
先找出所有有效的数据点,然后用interp1来计算出缺失点的值。
Problem 72. Interpolator
% You have a two vectors, a and b. They are monotonic and the same length. Given a value, va, where va is between a(1) and a(end) find the a(n), a(n+1) that flank it. Now interpolate the value, vb, such that it is proportionally between b(n) and b(n+1).
%
% va can land exactly on a value of