How to make a function of a NaN filter

6 visualizaciones (últimos 30 días)
Mania Mamakon
Mania Mamakon el 20 de Feb. de 2022
Editada: dpb el 20 de Feb. de 2022
I have the following code where 'vectordata' is whatever data needs to be filtered for NaNs. I just dont quite know how functions work and how to turn this into a function that I can call up whenever a vector needs to be filtered. Thanks in advance!
if isnan(vectordata(1)) == 1
vectordata(1) = 0;
end
for n = 2:length(vectordata)
if isnan(vectordata(n)) == 1
vectordata(n) = vectordata(n-1);
end
end

Respuesta aceptada

Image Analyst
Image Analyst el 20 de Feb. de 2022
Why not just use interp1()? Something like:
% Setup
v = randi(99, 1, 30);
x = 1 : length(v);
nanIndexes = randperm(length(v), 7);
v(nanIndexes) = nan
v = 1×30
69 47 NaN 15 6 96 29 43 85 34 36 81 50 36 94 34 48 NaN NaN 21 NaN 28 75 16 18 93 NaN 68 NaN NaN
% Now repair
goodIndexes = ~isnan(v);
v = interp1(x(goodIndexes), v(goodIndexes), x)
v = 1×30
69.0000 47.0000 31.0000 15.0000 6.0000 96.0000 29.0000 43.0000 85.0000 34.0000 36.0000 81.0000 50.0000 36.0000 94.0000 34.0000 48.0000 39.0000 30.0000 21.0000 24.5000 28.0000 75.0000 16.0000 18.0000 93.0000 80.5000 68.0000 NaN NaN
  3 comentarios
Image Analyst
Image Analyst el 20 de Feb. de 2022
OK, then to turn that code into a function you need to create an m-file called FilterVector.m and have this inside of it:
function vectordata = FilterVector(vectordata)
if isnan(vectordata(1)) == 1
vectordata(1) = 0;
end
for n = 2:length(vectordata)
if isnan(vectordata(n)) == 1
vectordata(n) = vectordata(n-1);
end
end
Mania Mamakon
Mania Mamakon el 20 de Feb. de 2022
Yes this worked! Thank you.

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 20 de Feb. de 2022
Editada: dpb el 20 de Feb. de 2022
@Image Analyst showed an effective way to solve the problem, I'll show an alternative and make a couple comments on MATLAB coding style...
if isnan(vectordata(1)) == 1
should be written as
if isnan(vectordata(1))
without the explicit "==1". isnan() returns a logical and the result of a logical is what if evaluates/branches on; all the addition of the test does is duplicate the same comparison and produce another logical variable result.
For myself, if I do have such a one-liner, I'll typically write such an expression as
if isnan(x), x=0; end
where "x" is whatever epression; above, of course it is 'vectordata(1)'
The preceding element interpolation can also be written in MATLAB with vector logical addressing as
if isnan(x(1)), x(1)=0; end % fixup the first entry so can't be NaN
isBad=isnan(x); % get the bad locations left
x(isBad)=x(find(isBad)-1); % and substitute the preceding in bad location
Nota Bene: The last expression on the RHS converts the logical index array over 1:N to positions in the vector that will be the position just to the left of the logical array position. Another way to write that which is equivalent in functionality but more obtuse to see would be
if isnan(x(1)), x(1)=0; end % fixup the first entry so can't be NaN
isBad=isnan(x); % get the bad locations left
x(isBad)=x(circshift(isBad,-1)); % move the bad locations one position left
Finally, the promised note on length() -- be wary of using length(). It returns max(size(x)) which is insensitive to orientation of a 2D array; if one uses it on an array of 3x5, say, thinking of wanting the rows, it will return the value 5 instead. If it is known the argument is a vector for certain, then it will always return the right/expected result, just "there be dragons!" otherwise that have caught the neophyte multiple times...
Oh, "Finally Second" -- how to turn into a function -- the latter above would simply be packaged as
function x=myInterpFunction(x)
% myInterFunction(X) replaces NaN elements in input vector X with those
% one position to the left in the input vector X
if isnan(x(1)), x(1)=0; end % fixup the first entry so can't be NaN
isBad=isnan(x); % get the bad locations left
x(isBad)=x(circshift(isBad,-1)); % move the bad locations one position left
return
Obviously some error check for vector input and the like, but that's all there is to it...

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by