replacing nan values with the closest pixel value

12 次查看(过去 30 天)
I have to matrices
A=[ 2 3 4 5 6 7 ; 2 4 nan 6 7 8; 4 nan 6 7 8 8] B=[nan nan nan 5 6;nan nan nan 4 5 nan;nan 6 nan nan 7 nan]
for every non nan value in A if B's corresponding values is nan then replace it with its closest number.
As I have got many nan values in B I am not sure how to replace it with the closest value.

回答(2 个)

Chad Greene
Chad Greene 2015-3-18
编辑:Chad Greene 2015-3-18
For clarity, can you edit your question to remove the three instances of the word "it"? This isn't me being a stickler--I just want to make sure I understand your question correctly, and I can interpret a few different meanings depending on what "it" refers to.
Another clarification needed: what do you mean by closest value? Do you mean rounded to some nearest value or do you mean nearest neighbor in the arrangement of the matrix?
It sounds like you'll want a two-step process. First, replace the NaN elements of B with corresponding elements of A:
A=[ 2 3 4 5 6 7 ;
2 4 nan 6 7 8;
4 nan 6 7 8 8];
B=[nan nan nan 5 6 nan;
nan nan nan 4 5 nan;
nan 6 nan nan 7 nan];
% Replace NaNs in B with corresponding values of A:
B(isnan(B)) = A(isnan(B))
B =
2 3 4 5 6 7
2 4 NaN 4 5 8
4 6 6 7 7 8
There's still one remaining NaN. One way to replace it is with John D'Errico's inpaint_nans function:
% inpaint remaining NaNs:
B = inpaint_nans(B)
B =
2.0000 3.0000 4.0000 5.0000 6.0000 7.0000
2.0000 4.0000 4.2222 4.0000 5.0000 8.0000
4.0000 6.0000 6.0000 7.0000 7.0000 8.0000
  3 个评论
Chad Greene
Chad Greene 2015-3-18
You'll have to choose which nearest neighbor. A NaN may have a finite value above, below, to its left, or to its right.
Here's a clunky solution that is somewhat prone to errors, but it works for this example:
B(isnan(B)) = A(isnan(B));
B(isnan(B)) = A(find(isnan(B))+1);
Hana
Hana 2015-3-18
your code replaces the nan values in B by the values in A..which is not what I want. I want B to become like: B=[5 5 5 5 6 6 ;4 4 4 4 5 5 ;6 6 6 7 7 7]

请先登录,再进行评论。


Guillaume
Guillaume 2015-3-18
According to your latest comment: I want B to become like: B=[5 5 5 5 6 6 ;4 4 4 4 5 5 ;6 6 6 7 7 7], where does A figure in this?
Anyway, here is a way to replace nans in a vector with the nearest (by position) non-nan value in the vector:
function v = nanreplace(v)
%replace nans in vin by the nearest non-value by position
nonnan = ~isnan(v);
vvalues = v(nonnan);
%bsxfun compute the distance from every nan to all non-nan.
%min then find which of the distance is the smallest
[~, mindistcol] = min(abs(bsxfun(@minus, 1:numel(v), find(nonnan)')));
%the row at which the minimum was picked is then the index of the non-nan value that is closest
v = mindistcol(vvalues);
end
To apply that to a whole matrix:
>>B = [nan nan nan 5 6 nan
nan nan nan 4 5 nan
nan 6 nan nan 7 nan]
>>cell2mat(cellfun(@nanreplace, num2cell(B, 2), 'UniformOutput', false))
ans =
5 5 5 5 6 6
4 4 4 4 5 5
6 6 6 7 7 7
  1 个评论
Morteza
Morteza 2020-6-10
I guess tha last line:
v = mindistcol(vvalues);
must be changed to
v = vvalues(mindistcol);
% Are you agree?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 NaNs 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by