How to make a function of a NaN filter
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
@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...
카테고리
도움말 센터 및 File Exchange에서 Descriptive Statistics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!