How to find closest values within a matirx
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi have a matirx
[NAN NAN NAN 7 8 9;
NAN NAN 5 7 8 7;
4 8 6 8 3 4;
4 7 0 1 1 4]
How do I replace the NAN values with the closest values known values. So the matirx will kind of look like
[4 5 7 7 8 9;
4 8 5 7 8 7;
4 8 6 8 3 4;
4 7 0 1 1 4]
4 comentarios
Steven Lord
el 29 de Abr. de 2022
What are your rules for "closest"? For element (1, 3) you fill in the NaN with the value from (1, 4) rather than the value from (2, 3). But for element (2, 2) you fill in with the value from (3, 2) rather than the value from (2, 3). Why do you choose the element to the side in the first case but the element below in the second?
Respuesta aceptada
Matt J
el 29 de Abr. de 2022
A=[nan(1,3) 7 8 9;
nan(1,2) 5 7 8 7;
4 8 6 8 3 4;
4 7 0 1 1 4]
nanmap=isnan(A);
[~,idx]=bwdist(~nanmap);
A(nanmap)=A(idx(nanmap))
Más respuestas (1)
DGM
el 29 de Abr. de 2022
Editada: DGM
el 29 de Abr. de 2022
I'm not really sure what you mean by "closest values". Maybe you're talking about inpainting?
A = [NaN NaN NaN 7 8 9;
NaN NaN 5 7 8 7;
4 8 6 8 3 4;
4 7 0 1 1 4]
B = regionfill(A,isnan(A))
If you don't have regionfill() (Image Processing Toolbox), you can always use John's inpaint_nans() on the File Exchange
C = inpaint_nans(A)
You might also be able to use fillmissing() depending on the directional behavior you intend.
2 comentarios
Ver también
Categorías
Más información sobre Filter Banks en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!