a small problem with interpolation
1 view (last 30 days)
Show older comments
Dear all,
I have
A={ [NaN] [ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
[4.8455] [0.8076] [0.0864] [ 0.1917] [ 0.5184] [ 0.4275]
[5.1224] [0.8537] [0.3679] [ 0.7887] [ 2.2073] [ 1.8883]
[5.1932] [0.8656] [0.6473] [ 1.3217] [ 3.8835] [ 3.3626]
[5.2023] [0.8671] [0.8252] [ 1.6275] [ 4.9510] [ 4.2929]
[5.1816] [0.8636] [0.7828] [ 1.6663] [ 4.6970] [ 4.0567]
[5.1818] [0.8636] [0.6551] [ 1.4768] [ 3.9308] [ 3.3937]
[5.1937] [0.8656] [0.6070] [ 1.4121] [ 3.6422] [ 3.1528]
[5.1665] [0.8611] [0.6443] [ 1.5050] [ 3.8656] [ 3.3284]
[5.1786] [0.8631] [0.6824] [ 1.5289] [ 4.0944] [ 3.5341]
[5.0321] [0.8386] [0.6997] [ 1.5754] [ 4.1985] [ 3.5209]
[4.9381] [0.8231] [0.7431] [ 1.6090] [ 4.4583] [ 3.6695]
[4.9326] [0.8221] [0.7708] [ 1.7676] [ 4.6245] [ 3.8018]
[4.7815] [0.7970] [0.8851] [ 2.0084] [ 5.3107] [ 4.0625]
[3.9136] [0.6523] [2.5109] [ 5.3401] [15.0656] [ 9.7846]
[3.8419] [0.6403] [4.3700] [ 8.2200] [26.2199] [16.7885]
[3.8559] [0.6426] [4.3385] [ 7.9125] [26.0312] [16.7235]
[3.8803] [0.6467] [3.9299] [ 7.9730] [23.5793] [15.2499]
[3.8741] [0.6457] [3.8246] [ 8.2150] [22.9478] [14.8167]
[3.8733] [0.6456] [3.9232] [ 8.2825] [23.5394] [15.1959]
[3.8604] [0.6434] [3.9364] [ 8.4879] [23.6182] [15.1961]
[3.8052] [0.6342] [3.9610] [ 8.4783] [23.7659] [15.0720]
[3.8177] [0.6363] [4.0807] [ 8.3607] [24.4842] [15.5765]
[3.7615] [0.6269] [5.4267] [10.9853] [32.5598] [20.4115]
[3.6900] [0.6150] [5.8236] [12.4549] [34.9416] [21.4899]
[3.7249] [0.6208] [6.0688] [12.3084] [36.4125] [22.6080]
[3.7621] [0.6270] [6.4914] [12.5395] [38.9481] [24.4177]
[3.7485] [0.6248] [7.1611] [12.7942] [42.9669] [26.8452]
[3.7492] [0.6249] [7.1063] [12.8555] [42.6379] [26.6462]
[3.7029] [0.6171] [6.6088] [12.9124] [39.6529] [24.4730]
[3.7172] [0.6195] [6.3574] [12.7681] [38.1441] [23.6259]
[3.8148] [0.6358] [6.2090] [12.7283] [37.2539] [23.6851]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN] [ NaN]}
I want to extrapolate the values in the last row.
I tried
out3=[];
outtx=CELL2MAT(A)
for c = 1:size(outtx,2)
outtxa=inpaint_nans(outtx(:,c)',2);
outtxa=outtxa';
end
out3=[out3 outtxa ];
but I get the following error
?? Subscript indices must either be real positive integers or logicals.
Error in ==> inpaint_nans at 233
fda(n,[n, n-1,n+n])=[-2 1 1];
thanks
PS: is there also a way to find the values in the first 4 rows?
1 Comment
Oleg Komarov
on 11 Aug 2012
Edited: Oleg Komarov
on 11 Aug 2012
Please, store your A in a double matrix.
A = cell2mat(A);
And keep it as double. Storing a double matrix in that ways wastes memory, it's annoying since you always have to convert with cell2mat and it's harder to work with.
Accepted Answer
Oleg Komarov
on 11 Aug 2012
Edited: Oleg Komarov
on 11 Aug 2012
It appears to be a bug in this contribution.
You have two options:
- report this to the author on the FEX and wait for HIS answer.
- Use interp1() with the 'extrap' option
% From my previous answer to your post
xi = (1:size(A,1))';
% Index the non NaN
idx = ~isnan(A(:,1));
out = interp1(xi(idx),A(idx,:),xi,'linear','extrap');
5 Comments
Oleg Komarov
on 11 Aug 2012
@Sabbas: which A are you using, not the same in the example?
I suppose you're looping because NaNs appear at different locations, if not, then no need to loop.
Here follows the polished code from your first comment:
A = cell2mat(A);
xi = (1:size(A,1))';
for c = 1:size(A,2)
idx = ~isnan(outtx(:,c));
outtx(:,c) = interp1(xi(idx),A(idx,c),xi,'linear','extrap');
end
It doesn't work because some of your series could be all NaN or have just one data point.
When it error check the index of the loop and control that the column has at least 2 data points. However, I would recommend checking this BEFORE applying interpolation. Generally, series which do not have enough observations are discarded in toto.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!