How do interpolate and hold values inside a vector?
Show older comments
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
Accepted Answer
More Answers (1)
Steven Lord
on 3 Jan 2019
0 votes
If your "NA" values are stored in the double array as NaN, use fillmissing with the 'previous' method.
Categories
Find more on Logical 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!