filling in blank cells with nearest number

1 回表示 (過去 30 日間)
John
John 2013 年 3 月 2 日
Hi there,
Would anybody be able to help me with some code to 'fill in some data'.
I have a column of data with '1's and '0's with blank cell in between. I'm trying to fill in the blank cells with the nearest number.
Greatly appreciate any help. I have 105,841 rows.
Here is an example:
  2 件のコメント
Wayne King
Wayne King 2013 年 3 月 2 日
how are these "blank cells" rendered in MATLAB as NaN?
John
John 2013 年 3 月 2 日
Hello Wayne, thanks for your reply. Yes they are rendered as NaN.
Regards

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

回答 (1 件)

Wayne King
Wayne King 2013 年 3 月 2 日
The question I'm not sure of from your post is how you want to deal with
1 NaN 0
0 NaN 1
Do you want the above to be assigned 1?
I'll assume that you have at most 1 NaN between numbers.
There are many ways to do this
x = [0 0 0 NaN 0 1 1 NaN 0];
indexvec = 1:length(x);
idx = isnan(x);
indexvec = indexvec(idx>0);
x(indexvec) = max(x(indexvec-1),x(indexvec+1));
The above code assigns a 1 to both
1 NaN 0
and
0 NaN 1
It also assigns 0 to
0 NaN 0
and 1 to
1 NaN 1
  4 件のコメント
John
John 2013 年 3 月 2 日
編集済み: John 2013 年 3 月 2 日
Hello, my apologies for not defining what I want clearly. What I am looking for is, when a long list of NaNs is buffeted by different values, I want to make half of them 1s and the other half 0s.
For example 5-8 are buffeted by a 1 and 0. I need to assign 5,6 as 0 and 7,8 as 1. This example is easier as there are 4 (an even) number of NaNs.
For example with an uneven number of NaNs, 16-20, are buffeted by a 1 and 0. I need to assign 16,17,18 as 1 and 19,20 as 0. The rule is the case of an uneven number of NaNs is to let middle cell be a 1.
Thank you
John
John 2013 年 3 月 2 日
Hello Wayne,
Sorry for bothering you, I'm just wondering if you had time to consider my problem any further.
Kind Regards

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

カテゴリ

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