2 views (last 30 days)

Matt J
on 27 Jul 2019

Edited: Matt J
on 27 Jul 2019

One way:

result = sqrt(cummax(M,1).*cummax(M,1,'reverse'))

Matt J
on 27 Jul 2019

dpb
on 27 Jul 2019

for i=1:size(M,2)

ix=find(M(:,i)==3);

if numel(ix)>1

M(ix(1):ix(end),i)=3;

end

end

dpb
on 27 Jul 2019

LOL! I knew that was coming while writing the above...illustrates that over-simplification gets the right answer to the wrong question.

How large are your arrays and what are actual values in real application? Such pattern matching may well be better suited to casting the values to char() as then can search for string match as patterns...

Andrei Bobrov
on 27 Jul 2019

Edited: Andrei Bobrov
on 30 Jul 2019

s = size(M);

[a,b] = regexp(join(string(M)',''),'30+3');

jj = repelem(1:s(2),cellfun(@numel,a));

lo = zeros(s);

lo(sub2ind(s,[a{:}]+1,jj)) = 1;

lo(sub2ind(s,[b{:}],jj)) = -1;

M(cumsum(lo)>0) = 3;

Other variant:

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];

m = M;

m(m == 0) = nan;

M(fillmissing(m,'previous') == 3 & fillmissing(m,'next') == 3) = 3;

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/473605-how-to-use-linear-interpolation-for-filling-with-3s-inside-empty-spaces-in-a-matrix-of-os-and-3s#comment_728690

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/473605-how-to-use-linear-interpolation-for-filling-with-3s-inside-empty-spaces-in-a-matrix-of-os-and-3s#comment_728690

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/473605-how-to-use-linear-interpolation-for-filling-with-3s-inside-empty-spaces-in-a-matrix-of-os-and-3s#comment_728913

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/473605-how-to-use-linear-interpolation-for-filling-with-3s-inside-empty-spaces-in-a-matrix-of-os-and-3s#comment_728913

Sign in to comment.