How do interpolate and hold values inside a vector?

3 Ansichten (letzte 30 Tage)
Sridutt Gokul
Sridutt Gokul am 3 Jan. 2019
Bearbeitet: Sridutt Gokul am 3 Jan. 2019
I have a vector that looks like this:
NA
NA
-33.20
-37.98
-40.83
-52.36
NA
-28.42
NA
NA
NA
-24.74
NA
NA
NA
-26.33
NA
NA
NA
-23.10
NA
NA
NA
-25.04
NA
NA
I am trying to build a function that can replace the NAs in the above vector using the following cases:
  • from the beginning, in which case I hold the first non NA value (-33.20 for the first two NAs here)
  • in the middle, where I interpolate because I have a start and stop value (between -52.36 and -28.42; between -28.42 and -24.72 and so on...)
  • towards the end, where I hold the last non NA value(-25.04 for the last two NAs)
I am relatively new to Matlab and this is what basic structure I have so far:
count = 0;
interp = 0;
hold = 0;
for cols = 1:34
for rows = 1:26
if (HMF(rows,cols)~=NA)
start=HMF(rows,cols);
end
if (HMF(rows,cols)==NA)
count = count + 1;
interp=1;
if(rows==1||rows==26)
hold=1;
interp=0;
end
end
stop = HMF(rows,cols);
if(interp == 1 && hold ==0)
%interpolate between start and stop
elseif (hold==1 && interp ==0)
%hold value of last known element
end
end
end

Akzeptierte Antwort

Michael C.
Michael C. am 3 Jan. 2019
Bearbeitet: Michael C. am 3 Jan. 2019
See if something like this is what you are looking for:
input = [nan; nan;-33.20; -37.98; -40.83;...
-52.36; nan; -28.42; nan; nan; nan;...
-24.74; nan; nan; nan; -26.33; nan;...
nan; nan; -23.10; nan; nan; nan; ...
-25.04; nan; nan];
% Intermediate step will interpolate to fill inner NaN's
intermediate = interp1(find(~isnan(input)),input(~isnan(input)),1:numel(input));
% Second interp will fill outer NaNs with the closest real value
output = interp1(find(~isnan(intermediate)),intermediate(~isnan(intermediate)),...
1:numel(intermediate),'nearest','extrap');
Essentially, the first interpolation is just linear and will fill in the NaNs that have values on either side. If you look at the output of this step, you will see that the NaNs remain on the edges.
The second interp1 call gets rid of the outer NaNs using the 'extrap' option, but also just fills them in with the nearest good value (which is what the 'nearest' option does)
This does make the assumption that your inputs are equally spaced. If that is not hte case, you would need to replace the first and last inputs into the interp1 function with something that describes the spacing of the data points.
Note: Edited output line to correct typo
  4 Kommentare
Sridutt Gokul
Sridutt Gokul am 3 Jan. 2019
Thank you, this works exactly as imagined.
Sridutt Gokul
Sridutt Gokul am 3 Jan. 2019
Bearbeitet: Sridutt Gokul am 3 Jan. 2019
I have one more question, this method would break in case my input is
input = [NaN;NaN;NaN;-44.1243030692308;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN]
since there is only one value, is there anyway to see the same method for this type of input?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Steven Lord
Steven Lord am 3 Jan. 2019
If your "NA" values are stored in the double array as NaN, use fillmissing with the 'previous' method.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by