Interpolation of zero values ​​in matlab

Hello! The readings from the gas sensor look like this: 70 71 0 69 0, at a temperature of 12 12.2 12.1 11.9 11.2. I try to interpolate all zero values, but for some reason I don’t consider it at all
[cat,nocat]=min(Animals); %readings from a 1x1200 thermometer
[dog,nodog]=max(Animals); % gas sensor reading 1x1200
for i=1:length(cat)
if dog(i)<0 % if some indication is less than 0 (they are all positive)
interpol(i)=dog(i); % the location of these zeros and this is what we interpolate (1 and the last value are not zero)
c(i)=interp1(interpol(i),cat(i),nearest); % interpolate;
else(i) dog(i)>0 % if the values ​​are greater than 0
c(i)= dog(i); % That testimony and remain
end
end

 Accepted Answer

Alex Mcaulley
Alex Mcaulley on 18 Jul 2019
Edited: Alex Mcaulley on 18 Jul 2019
Try this: (Note that "cat" is not a good variable name because it is a Matlab function)
[cat,nocat]=min(Animals); %readings from a 1x1200 thermometer
[dog,nodog]=max(Animals); % gas sensor reading 1x1200
vq = interp1(cat(dog>0),dog(dog>0),cat(dog<=0),'nearest','extrap'); %nearest or other interpolation method you want
dog(dog<=0) = vq;

6 Comments

Error The grid vectors must contain unique points.
If you use the sample you provided it works. The error is due to have repeated points (points with the same temperature in your case). See this reference to see what is happening and how to fix it.
[cat, dog] = unique(cat);
vq = interp1(cat(dog>0),dog(dog>0),cat(dog<=0),'nearest','extrap'); %nearest or other interpolation method you want
dog(dog<=0) = vq;
I did this and now vq in workspace is displayed as [] 1x0
And I do not like the values ​​of cats and dogs (they greatly decreased was 1200 and it became 1x732)
Try with this:
[cat,nocat]=min(Animals); %readings from a 1x1200 thermometer
[dog,nodog]=max(Animals); % gas sensor reading 1x1200
[newCat, index] = unique(cat);
newDog = dog(index);
vq = interp1(newCat(newDog>0),newDog(newDog>0),cat(dog<=0),'nearest','extrap'); %nearest or other interpolation method you want
dog(dog<=0) = vq;
Thank you, everything seems to work, but now I have 202 values. And I just wanted to replace my zeros in the dog with the values ​​of this interpolation, that would be 1x1200, all values ​​are positive numbers
What variable has 202 values? vq? That is right (it is the number of zeros in dog variable)
If you see the dog variable it should have the same number of elements than in the begining (1200) with non zero values.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!