I'm trying to write a linear interpolation script in matlab. How do I fix that the vectors are of different length?
I've tried to wrap my head around this issue, but can't seem to get it. Should an if statement be included anywhere in the for loop?
z = linspace(0,1000,21)
vel = 1500*z^0.1;
% I want to interpolate vel between the 201 elements of depths.
depths = [0:5:1000];
numel_depths = numel(depths);
for ii = 1:numel_depths %Going through all indices of depths
lower = find(z > depths(ii),1); % Finding x0 and x1.
higher = find(z < depths(ii),1,'last');
V2(ii) = vel(lower) + ((vel(higher) - vel(lower))/(z(higher)-
z(lower)))*(depths(ii)-z(lower)); % linear interpolation step
end
Now it returns an error saying that the different sides had different number of elements. Is there a way to fix this so that it works as the interp1 function already installed in MATLAB?