How to replace a NaN element with an preceeding element in a matrix using logical indexing?

4 ビュー (過去 30 日間)
Ganeshkumar M
Ganeshkumar M 2017 年 2 月 28 日
回答済み: Jos (10584) 2017 年 3 月 2 日
Hi I have a 300x200 matrix with numbers ranging from 0 to 3. I also have NaN interspersed within the matrix like
[2 1 1 1 1 1 1 2 2 2 2 2;
2 2 3 3 3 N 2 3 3 3 0 0;
2 2 0 N 1 2 1 N 1 1 2 3;] NaN represented as N
is there a way to use logical indexing to replace the elements NaN to the number that precedes it? and perhaps to replace Nan with the element that occurs in the highest frequency out of the preceding 3 elements? Thank you!
I can use if and for loop but would prefer to keep the time short in favour of speed. Any use if I could use isnan to create the matrix to identify NaN first?
  1 件のコメント
Jos (10584)
Jos (10584) 2017 年 2 月 28 日
Your question is too ill-defined for a proper answer yet.
  • What do you mean by preceding (same row, column before)?
  • What if there are two or more NaNs in a row?
  • What if the row starts with a NaN?
  • What if there is no mode value (mode = value that occurs the most)?

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

回答 (3 件)

Jos (10584)
Jos (10584) 2017 年 2 月 28 日
To replace NaNs by the preceding value in the row, assuming no NaN's in the first column:
A = [ 2 1 1 1 1 1 1 2 2 2 2 2
2 2 3 3 111 NaN 2 3 3 3 0 0
2 2 222 NaN 1 2 333 NaN 1 1 2 3]
S2Ifun =@(x,y) sub2ind(size(A), x, y) % an anonymous helper function
[r,c] = find(isnan(A)) % where are the NanN's
A(S2Ifun(r,c)) = A(S2Ifun(r,c-1))
  5 件のコメント
Ganeshkumar M
Ganeshkumar M 2017 年 3 月 2 日
Hoping to replace those NaNs with the number that precedes it. so for example in row 4, there is a long stretch of Nans, it should hopefully take on the value at the start of the row which is 2.
Jos (10584)
Jos (10584) 2017 年 3 月 2 日
And what about a NaN in the first column?

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


Chris Turnes
Chris Turnes 2017 年 3 月 1 日
As far as replacing with the previous non-NaN element, you could check out the fillmissing function that was introduced in R2016b. The fill method 'previous' will replace NaNs with the previous non-NaN element.
  2 件のコメント
Ganeshkumar M
Ganeshkumar M 2017 年 3 月 2 日
Im using matlab 2013. any ways i can download the toolbox?
Ganeshkumar M
Ganeshkumar M 2017 年 3 月 2 日
This code will be the best. Came across fillts and inpaint_nans but they use interpolation instead of taking the previous.

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


Jos (10584)
Jos (10584) 2017 年 3 月 2 日
If you can replace the NaNs with a zeros, you can use my function FILLZERO. If there are zeros in your matrix you can replace them first with a novel value, and later replace them as well.
NovelValue = max(A(:)) + 1 % find a value not in A
A(A==0) = NovelValue % replace zeros
A(isnan(A)) = 0
A = fillzero(A,2)
A(A==NovelValue) = 0 % restore zeros
You can find my FILLZERO function here:

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by