Filter löschen
Filter löschen

replacing nan values with the closest pixel value

10 Ansichten (letzte 30 Tage)
Hana
Hana am 18 Mär. 2015
Kommentiert: Morteza am 10 Jun. 2020
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.

Antworten (2)

Chad Greene
Chad Greene am 18 Mär. 2015
Bearbeitet: Chad Greene am 18 Mär. 2015
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 Kommentare
Chad Greene
Chad Greene am 18 Mär. 2015
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 am 18 Mär. 2015
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]

Melden Sie sich an, um zu kommentieren.


Guillaume
Guillaume am 18 Mär. 2015
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 Kommentar
Morteza
Morteza am 10 Jun. 2020
I guess tha last line:
v = mindistcol(vvalues);
must be changed to
v = vvalues(mindistcol);
% Are you agree?

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by