How to use linear interpolation for filling with 3s inside empty spaces in a matrix of os and 3s
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
M = [0 0 0;... 0 0 3;... 3 3 0;... 0 3 3;... 3 0 0;... 0 0 3;... 3 0 0;... 0 0 0]; I want to use interpolation to fill the gaps between 3s. I tried different methods but no satisfactory answer Is there any other method possible to apply plz Thanks for all cooperation
2 commentaires
dpb
le 26 Juil 2019
Show us what you tried on a real array and what you think wrong with the answer got...
I have no idea what it is you have in mind from this description, sorry...
Réponses (3)
Matt J
le 27 Juil 2019
Modifié(e) : Matt J
le 27 Juil 2019
One way:
result = sqrt(cummax(M,1).*cummax(M,1,'reverse'))
2 commentaires
Matt J
le 27 Juil 2019
My answer addresses your posted question. I suggest you Accept-click one of the answers given to you and then post a new question that covers your more complicated M.
dpb
le 27 Juil 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
2 commentaires
dpb
le 27 Juil 2019
Modifié(e) : dpb
le 27 Juil 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
le 27 Juil 2019
Modifié(e) : Andrei Bobrov
le 30 Juil 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;
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!