How to find closest values within a matirx

4 ビュー (過去 30 日間)
A
A 2022 年 4 月 29 日
コメント済み: A 2022 年 4 月 30 日
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 件のコメント
Steven Lord
Steven Lord 2022 年 4 月 29 日
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?
A
A 2022 年 4 月 29 日
Yea it kinda like that logic, like nearest neighbors almost. Becasue for (2,2) the values is close is (3,2) rather than (2,3)

サインインしてコメントする。

採用された回答

Matt J
Matt J 2022 年 4 月 29 日
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]
A = 4×6
NaN NaN NaN 7 8 9 NaN NaN 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))
A = 4×6
4 5 5 7 8 9 4 8 5 7 8 7 4 8 6 8 3 4 4 7 0 1 1 4
  1 件のコメント
A
A 2022 年 4 月 30 日
That makes more sense, thank you!!

サインインしてコメントする。

その他の回答 (1 件)

DGM
DGM 2022 年 4 月 29 日
編集済み: DGM 2022 年 4 月 29 日
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]
A = 4×6
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))
B = 4×6
5.4778 5.8000 5.9333 7.0000 8.0000 9.0000 5.1556 5.9889 5.0000 7.0000 8.0000 7.0000 4.0000 8.0000 6.0000 8.0000 3.0000 4.0000 4.0000 7.0000 0 1.0000 1.0000 4.0000
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 件のコメント
A
A 2022 年 4 月 29 日
No like creating new values, kind of like subsituing the already known values into the NAN. So kinda like nearest neighbor, if we know what its closes elements are then we subisite them into the NAN
DGM
DGM 2022 年 4 月 29 日
Then Matt's answer might be more what you want

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePolynomials についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by