Skip to content

Instantly share code, notes, and snippets.

@mikofski
Last active December 12, 2015 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikofski/4758464 to your computer and use it in GitHub Desktop.
Save mikofski/4758464 to your computer and use it in GitHub Desktop.
get rid of nan (or inf,-9999,0 or any flag) using circshift

Here's an example:

>> [data,shifts] = smooth_flag_wlast_nonflag(data_copy,NaN,true)

    0:     4    5  NaN    1    2    3  NaN  NaN  NaN    5    3  NaN  NaN    3
    1:     4    5    5    1    2    3    3  NaN  NaN    5    3    3  NaN    3
    2:     4    5    5    1    2    3    3    3  NaN    5    3    3    3    3
    3:     4    5    5    1    2    3    3    3    3    5    3    3    3    3
data =

     4     5     5     1     2     3     3     3     3     5     3     3     3     3


shifts =

     3

>> 
function [data,shifts] = smooth_flag_wlast_nonflag(data,flag,display)
if nargin<2 || isempty(flag)
flag = NaN;
end
if nargin<3 || isempty(display)
display = false;
end
if isnan(flag)
isflagged = @(x)isnan(x);
elseif isinf(flag)
isflagged = @(x)isinf(x);
else
isflagged = @(x)x==flag;
end
flagged_data = isflagged(data);
shifts = 0;
if display
fprintf('\n%5d: ',shifts)
fprintf('%5d',data)
end
while any(flagged_data)
shifts = shifts+1;
dummy = circshift(data,[0,1]);
data(flagged_data) = dummy(flagged_data);
flagged_data = isflagged(data);
if display
fprintf('\n%5d: ',shifts)
fprintf('%5d',data)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment