function to fill in zeros

5 vues (au cours des 30 derniers jours)
Lev Mihailov
Lev Mihailov le 2 Sep 2019
Hello! I have an array of data where there are zeros and I need to replace them. How can I do this?
data=[100 95 0 90 0 85 0 0 70 65 ] % as it is
data=[100 95 92.5 90 87.5 85 ] % and so it is necessary
is there a special function in matlab for this?
  5 commentaires
Lev Mihailov
Lev Mihailov le 2 Sep 2019
Well, actually, I need
data=[2 0 1]
data=[2 1.5 1]
Adam
Adam le 2 Sep 2019
I'm guessing you want to replace 0s with the mean of the values either side of them, but as madhan ravi says, if you cut off a fundamental part of the example it's impossible to know. Replacing individual zeros with the mean of values either side is easy. Replacing zeros when you get multiple of them in a row is a little less clearly defined.

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 2 Sep 2019
Modifié(e) : Andrei Bobrov le 2 Sep 2019
data=[100 95 0 90 0 85 0 0 70 65 ]';
data(data == 0) = nan;
new_data = fillmissing(data,'linear');
or
ii = (1:numel(data));
lo = data ~= 0;
new_data = interp1(ii(lo),data(lo),ii);

Plus de réponses (0)

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!

Translated by