Linear FillMissing producing incorrect values?

1 view (last 30 days)
My Code involves filling in missing values using linear interpolation, but its plain to see that the values its giving are wrong. User error or matlab bug? Instead of interpolating the missing value for 76.82, its simply halving the previous value and calling it a day. Thanks in advance guys!
Baye2 =
0 0
14.1800 NaN
29.0625 7.7096
58.1250 0
74.5625 4.3605
76.8200 NaN
91.0000 0
Bayee2 =
0 0
14.1800 3.8548
29.0625 7.7096
58.1250 0
74.5625 4.3605
76.8200 2.1803
91.0000 0
%Inputs%
%example from bottom horizontal on D11.10z%
clear
clc
DP = 38.2; %Design pressure in psf
ML = 91; %Mullion length in inches
B1W = 28.36; %Bay 1 width in inches
B2W = 60.67; %Bay 2 width in inches
B1BP = []; %Bay 1 break points in inches seperated by spaces
B2BP = [58.125]; %Bay 2 break points in inches seperated by spaces
B1 = [0 0];
B2 = [0 0];
if isempty(B1BP) %if there is only a single loading on bay 1 (no breakpoints)
if ML > B1W % Trapezoidal loading
B1 = [0 0; B1W/2 B1W/2/144*DP; ML-B1W/2 B1W/2/144*DP; ML 0];
else %Triangular loading
B1 = [0 0; ML/2 ML/2/144*DP; ML 0];
end
else %if there are multiple loadings for bay 1
B1BP = [0 B1BP ML];
for n = 1:length(B1BP)-1 %for each loading on bay 1
if (B1BP(n+1)-B1BP(n)) > B1W % Trapezoidal loading
Bn1s{n} = [B1BP(n) 0; B1BP(n)+B1W/2 B1W/2/144*DP; B1BP(n+1)-B1W/2 B1W/2/144*DP; B1BP(n+1) 0];
Bn1 = [B1BP(n) 0; B1BP(n)+B1W/2 B1W/2/144*DP; B1BP(n+1)-B1W/2 B1W/2/144*DP; B1BP(n+1) 0];
B1 = [B1; Bn1];
n=n+1;
else %Triangular loading
Bn1s{n} = [B1BP(n) 0; B1BP(n)+(B1BP(n+1)-B1BP(n))/2 (B1BP(n+1)-B1BP(n))/2/144*DP; B1BP(n+1) 0];
Bn1 = [B1BP(n) 0; B1BP(n)+(B1BP(n+1)-B1BP(n))/2 (B1BP(n+1)-B1BP(n))/2/144*DP; B1BP(n+1) 0];
B1 = [B1; Bn1];
n=n+1;
end
end
end
B1 = unique(B1,'rows');
if isempty(B2BP) %if there is only a single loading on bay 2 (no breakpoints)
if ML > B2W % Trapezoidal loading
B2 = [0 0; B2W/2 B2W/2/144*DP; ML-B2W/2 B2W/2/144*DP; ML 0];
else %Triangular loading
B2 = [0 0; ML/2 ML/2/144*DP; ML 0];
end
else %if there are multiple loadings for bay 2
B2BP = [0 B2BP ML];
for j = 1:length(B2BP)-1 %for each loading on bay 1
if (B2BP(j+1)-B2BP(j)) > B2W % Trapezoidal loading
Bn2s{j} = [B2BP(j) 0; B2BP(j)+B2W/2 B2W/2/144*DP; B2BP(j+1)-B2W/2 B2W/2/144*DP; B2BP(j+1) 0];
Bn2 = [B2BP(j) 0; B2BP(j)+B2W/2 B2W/2/144*DP; B2BP(j+1)-B2W/2 B2W/2/144*DP; B2BP(j+1) 0];
B2 = [B2; Bn2];
j=j+1;
else %Triangular loading
Bn2s{j} = [B2BP(j) 0; B2BP(j)+(B2BP(j+1)-B2BP(j))/2 (B2BP(j+1)-B2BP(j))/2/144*DP; B2BP(j+1) 0];
Bn2 = [B2BP(j) 0; B2BP(j)+(B2BP(j+1)-B2BP(j))/2 (B2BP(j+1)-B2BP(j))/2/144*DP; B2BP(j+1) 0];
B2 = [B2; Bn2];
j=j+1;
end
end
end
B2 = unique(B2,'rows');
B1L = B1(:,1); %extracts just the locations from the Bay 1 loadings
B1z = NaN(size(B1L,1),1); %creates a column of unknowns the length of the locations
B1L = [B1L B1z]; %combines these two columns
B2L = B2(:,1);
B2z = NaN(size(B2L,1),1);
B2L = [B2L B2z];
B1 = [B1;B2L];
B1 = sortrows(B1);
[Bay1, ia] = unique(B1(:,1),'rows');
Baye1 = B1(ia,:);
Bayee1 = fillmissing(Baye1,'linear');
B2 = [B2;B1L];
B2 = sortrows(B2);
[Bay2, ib] = unique(B2(:,1),'rows');
Baye2 = B2(ib,:)
Bayee2 = fillmissing(Baye2,'linear')
Loadings = [Bayee1(:,1) Bayee1(:, 2) + Bayee2(:, 2)];
Loadings = round(Loadings, 2);

Accepted Answer

the cyclist
the cyclist on 29 Jun 2023
Edited: the cyclist on 29 Jun 2023
User error (or, really, misinterpretation).
Although the result happens to be half, the process is not to just take half of the previous value. In the case of your first NaN, it's linearly interpolating between 0 and 7.7096, which gives 3.8548. It's only because one of your values is zero, that the result is half of the other one.
  5 Comments
the cyclist
the cyclist on 29 Jun 2023
Edited: the cyclist on 29 Jun 2023
You can:
Baye2 = [
0 0
14.1800 NaN
29.0625 7.7096
58.1250 0
74.5625 4.3605
76.8200 NaN
91.0000 0];
% What you did (uses implicit even spacing)
fillmissing(Baye2,'linear')
ans = 7×2
0 0 14.1800 3.8548 29.0625 7.7096 58.1250 0 74.5625 4.3605 76.8200 2.1803 91.0000 0
% How to use the first column of Baye2 as the sample points
fillmissing(Baye2,'linear','SamplePoints',Baye2(:,1))
ans = 7×2
0 0 14.1800 3.7616 29.0625 7.7096 58.1250 0 74.5625 4.3605 76.8200 3.7616 91.0000 0
You will need to be careful to not interpolate every column of your matrix, if that is not what you want to do.
Nathan Reed
Nathan Reed on 29 Jun 2023
That seems to be working great, thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!