filling in blank cells with nearest number
이전 댓글 표시
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
2013년 3월 2일
how are these "blank cells" rendered in MATLAB as NaN?
John
2013년 3월 2일
답변 (1개)
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
2013년 3월 2일
Wayne King
2013년 3월 2일
It's not that unusual, but please define what you want when a long list of NaNs is buffeted by different values, for instance, in your screen shot, should values 6-10 be assigned a 1 or 0, what about 5-8?
John
2013년 3월 2일
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!