# Rows interpolation in a matrix

4 views (last 30 days)
Carlos Alonso on 3 Jul 2024
Commented: Mathieu NOE on 4 Jul 2024
hi
I have a matrix that contents "zeros". I would like to do a row interpolation in those cells that have "zeros" . See example attached
(Note: I need a generic solution. The table below is just a reduced example).
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Accepted Answer

Umar on 3 Jul 2024

Hi Carlos,

To help achieve your goal, you can use linear interpolation to estimate the missing values. Create a sample matrix with zeros. Then, iterate over each row of the matrix. For rows containing zeros, find the indices of zero elements. Then, find the indices of non-zero elements in the row.Afterwards, use linear interpolation to estimate the missing values at zero indices based on neighboring non-zero values. Finally, display the original matrix and the interpolated matrix.

>> % Create a sample matrix with zeros matrix = [1 2 0 4 5; 0 2 3 0 5; 1 0 3 4 5; 1 2 3 4

% Display the original matrix disp('Original Matrix:'); disp(matrix);

% Perform row interpolation for i = 1:size(matrix, 1) row = matrix(i, :); zero_indices = find(row == 0);

```    if ~isempty(zero_indices)
non_zero_indices = find(row ~= 0);
row(zero_indices) = interp1(non_zero_indices, row(non_zero_indices), zero_indices, 'linear');
matrix(i, :) = row;
end
end```

% Display the interpolated matrix disp('Interpolated Matrix:'); disp(matrix);

By runnig this script in MATLAB with your matrix data, you will be able to perform row interpolation on cells with zeros and obtain the interpolated results.

##### 2 CommentsShow NoneHide None
Carlos Alonso on 3 Jul 2024
Thank you Umar, your solution works for me.
Umar on 3 Jul 2024
Moved: Torsten on 3 Jul 2024
Glad to help you out,Carlos. I also do appreciate Mathie’s efforts too. He is a great contributor. Let us know if you need further assistance from us in the future. Good luck with your future endeavors.

Sign in to comment.

### More Answers (1)

Mathieu NOE on 3 Jul 2024
Edited: Mathieu NOE on 3 Jul 2024
hello
you can use fillmissing for this task
data = readmatrix("data.txt");
data(abs(data)<eps) = NaN;
data_out = fillmissing(data,'linear',2) % NB we use dim = 2 in arguments
data_out = 6x15
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 176.0000 178.0000 180.0000 182.0000 184.0000 186.0000 188.0000 190.0000 192.0000 194.0000 196.0000 198.0000 200.0000 202.0000 204.0000 8.0000 8.5000 9.0000 9.5000 10.0000 10.5000 11.0000 11.5000 12.0000 12.5000 13.0000 13.5000 14.0000 14.5000 15.0000 12.0000 12.1000 12.2000 12.3000 12.4000 12.5000 12.6000 12.7000 12.8000 12.9000 13.0000 13.1000 13.2000 13.3000 13.4000 75.0000 76.0000 77.0000 78.0000 79.0000 80.0000 81.0000 82.0000 83.0000 84.0000 85.0000 86.0000 87.0000 88.0000 89.0000 96.0000 96.4000 96.8000 97.2000 97.6000 98.0000 98.4000 98.8000 99.2000 99.6000 100.0000 100.4000 100.8000 101.2000 101.6000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
##### 2 CommentsShow NoneHide None
Carlos Alonso on 3 Jul 2024
Thank you Mathieu. Your solution also works.
Mathieu NOE on 4 Jul 2024
my pleasure !

Sign in to comment.

### Categories

Find more on Interpolation in Help Center and File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!