MATLAB Answers

0

How to use Matlab to fill gaps

Asked by M.S. Khan on 27 Jul 2019
Latest activity Commented on by 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

Show 1 older comment
Dear M.k, I we have M matrix. How to fill the gaps column by column among 3s by 3s
I am using interpol1() ruction but it is not filling gaps
darova
on 27 Jul 2019
What is 'gap'?

Sign in to comment.

1 Answer

Matt J
Answer by Matt J
on 27 Jul 2019
 Accepted Answer

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

  1 Comment

Thanks Dear Matt. Warm regards

Sign in to comment.