How to make a function of a NaN filter
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mania Mamakon
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
0 comentarios
Respuesta aceptada
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
% Now repair
goodIndexes = ~isnan(v);
v = interp1(x(goodIndexes), v(goodIndexes), x)
3 comentarios
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
Más respuestas (1)
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...
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!