How to use Matlab to fill gaps

6 views (last 30 days)
M.S. Khan
M.S. Khan on 27 Jul 2019
Commented: M.S. Khan on 30 Jul 2019
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
M.S. Khan
M.S. Khan on 27 Jul 2019
I am using interpol1() ruction but it is not filling gaps
darova
darova on 27 Jul 2019
What is 'gap'?

Sign in to comment.

Accepted Answer

Matt J
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)

Community Treasure Hunt

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

Start Hunting!