How to fill in missing data?
    6 views (last 30 days)
  
       Show older comments
    
Hello everybody,
I have a dataset(txt file) which contains some missing values (represented with 0). I would like to replace all this 0 places with numbers. How can I do?
0 Comments
Answers (2)
  Ayush
      
 on 7 Oct 2024
        Hi,
One way to replace missing values currently represented as 0 is to use interpolation. Start by identifying the indices of the zeros in your dataset. Then, apply the “interp1” function to perform the interpolation. Refer to the pseudo code below for a better understanding:
% Step 1: Load the data
data = readmatrix('your_dataset.txt');
% Step 2: Interpolate to replace zeros
for col = 1:size(data, 2)
    x = 1:size(data, 1); % Indices of the data
    y = data(:, col);    % Data values
    % Find indices of non-zero and zero elements
    nonZeroIndices = y ~= 0;
    zeroIndices = y == 0;
    % Perform interpolation only if there are non-zero elements
    if any(nonZeroIndices)
        % Interpolate only non-zero elements
        yInterpolated = interp1(x(nonZeroIndices), y(nonZeroIndices), x(zeroIndices), 'linear', 'extrap');
        % Replace zeros with interpolated values
        y(zeroIndices) = yInterpolated;
    end
    % Update the column in the dataset
    data(:, col) = y;
end
% Step 3: Save the modified data back to a file (optional)
writematrix(data, 'modified_dataset.txt');
For more information on using the “interp1” function, please refer to the documentation below: 
0 Comments
  Star Strider
      
      
 on 7 Oct 2024
        T1 = array2table(randi([0 9],10,5))                                             % Original
loc = table2array(T1) == 0                                                      % Logical Matrix Of ‘0’ Locations
T1 = fillmissing(T1,  'linear', MissingLocations=loc, EndValues='nearest')      % 'linear' Interpolation Of Missing Values With 'nearest' For End Values
There are several methods to fill (interpolate) the missing values.  See the documentation for those and other options.  
.
0 Comments
See Also
Categories
				Find more on Interpolation 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!

