How to use Matlab to fill gaps
3 views (last 30 days)
Show older comments
If I have matrix in this shape. M =[0 0 0; 2 2 3; 3 3 0; 0 0 0; 3 3 0; 2 2 3; 0 0 0; 3 3 2; 0 0 0; 3 3 3] How can I fill: 3 0 0 3 —> 3 3 3 3 3 0 3 0 2 —> 3 3 3 0 2 3 0 3 2 0 3 0 3 —> 3 3 3 2 0 3 3 3 Regards in advance for sharing knowledge
4 Comments
Accepted Answer
Matt J
on 27 Jul 2019
This might be what you are looking for
Mnew=M;
[nr,nc]=size(M);
xq=(1:nr).';
for i=1:nc
m=M(:,i);
[x,~,y]=find(m);
vals=interp1(x,y,xq);
m((m==0)&(vals==3))=3;
Mnew(:,i)=m;
end
M, Mnew
M =
0 0 0
2 2 3
3 3 0
0 0 0
3 3 0
2 2 3
0 0 0
3 3 2
0 0 0
3 3 3
Mnew =
0 0 0
2 2 3
3 3 3
3 3 3
3 3 3
2 2 3
0 0 0
3 3 2
3 3 0
3 3 3
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!