How to fill empty matrix elements based on surrounding values
11 views (last 30 days)
Show older comments
Simon Allosserie
on 6 Apr 2022
Commented: Simon Allosserie
on 7 Apr 2022
I have a matrix with a lot of empty values and some numbers. A column could look like this (simplified for the sake of this question):
[1 0 0 0 5 0 0 0 0 0 8 0 3]'
The first and last element of the row is always non-zero.
Now I need to fill the matrix column per column, such that the zeros are replaced by linear interpolation values between the surrounding values as such:
[1 0 0 0 5 0 0 0 0 0 8 0 3]' %inital column
[1 2 3 4 5 5.5 6 6.5 7 7.5 8 5.5 3]' %filled column with linear interpolation
The way I would do it now is with loops and iterations: find a non-zero element, find the next non-zero element, replace the elements between those with linear interpolation values. Do this for all values in the column, and for each column.
This certainly is not the most optimal way of doing it. I don't see however how I could change this into a matrix operation, nor did I find a specific function or tool to do this. So I was wondering if anyone has suggestions on how optimise this, or knows a function that performs this operation?
0 Comments
Accepted Answer
More Answers (1)
Image Analyst
on 6 Apr 2022
You tagged it as "Image Processing". For an image, you might look at the function regionfill().
See Also
Categories
Find more on Image Segmentation and Analysis 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!