fill missing value in array

8 views (last 30 days)
michael
michael on 4 Jul 2023
Answered: KSSV on 4 Jul 2023
Hi,
(unfortunatly, I have old matlab, where timeseries is not existing).
I have an array with data data_array, from which I have selected indeces K which I'd like to use.
I need to generate a new array dest_array , which will be with same length as the original one and populate values to it so that:
If I have selected index k (from K), the value dest_array[k]=data_array[k].
For all the missing enteries, I need to fill with values from previous known one.
What is the best way to do it?

Accepted Answer

KSSV
KSSV on 4 Jul 2023
x = 1:100 ;
y = rand(size(x)) ;
% Introduce NaN's randomly
idx = randperm(100,20) ;
y0 = y ;
y(idx) = NaN ;
% Use interp1
yi = interp1(x(~isnan(y)),y(~isnan(y)),idx) ;
% compare
plot(y0(idx),'r')
hold on
plot(yi,'b')
legend('Original','filled')

More Answers (0)

Categories

Find more on Preprocessing Data in Help Center and File Exchange

Products


Release

R14SP2

Community Treasure Hunt

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

Start Hunting!