I have a 2d array (doubles
) representing some data, and it has a bunch of NaNs
in it. The contour plot of the data looks like this:
Okay without using your plot function, I can still give you a solution. What you want to do is find all the new NaN
's and replace it with the original unfiltered data (assuming it is correct). While it's not filtered, it's better than reducing the domain of your contour image.
% Toy Example Data
rfVals= rand(100,100);
rfVals(1:2,:) = nan;
rfVals(:,1:2) = nan;
% Create and Apply Filter
filtWidth = 3;
imageFilter=fspecial('gaussian',filtWidth,filtWidth);
dataFiltered = imfilter(rfVals,imageFilter,'symmetric','conv');
sum(sum(isnan( dataFiltered ) ) )
% Replace New NaN with Unfiltered Data
newnan = ~isnan( rfVals) & isnan( dataFiltered );
dataFiltered( newnan ) = rfVals( newnan );
sum(sum(isnan( rfVals) ) )
sum(sum(isnan( dataFiltered ) ) )
Detect new NaN
using the following code. You can also probably use the xor
function.
newnan = ~isnan( rfVals) & isnan( dataFiltered );
Then this line sets the indices in dataFiltered
to the values in rfVals
dataFiltered( newnan ) = rfVals( newnan );
Results
From the lines printed in the console and my code, you can see that the number of NaN
in dataFiltered is reduced from 688 to 396 as was the number of NaN
in rfVals
.
ans =
688
ans =
396
ans =
396
Alternate Solution 1
You can also use a smaller filter near the edges by specifying a smaller kernel and merging it after, but if you just want valid data with minimal code, my main solution will work.
Alternate Solution 2
An alternate approach is to pad/replace the NaN
values with zero or some constant you want so that it will work, then truncate it. However for signal processing/filtering, you will probably want my main solution.