Fill out zeros/NaN in array with next value

2 ビュー (過去 30 日間)
Christian Frandsen
Christian Frandsen 2016 年 12 月 5 日
コメント済み: Christian Frandsen 2016 年 12 月 6 日
Hi everybody
I have the following 2 dimensional matrix:
1 NaN NaN
2 1 NaN
NaN 2 1
2 NaN NaN
NaN NaN 2
NaN NaN 2
NaN 2 NaN
What I want is to replace the NaN with the first next value that is not NaN (Looking at the first dimension (column)
1 1 1
2 1 1
2 2 1
2 2 1
2 2 2
2 2 2
2 2 2
What do I do?

回答 (1 件)

Jim Hokanson
Jim Hokanson 2016 年 12 月 5 日
I'm not entirely sure how you are generating the output, but you should be able to do something like:
I = find(isnan(data));
for iNaN= 1:length(I)
cur_index = I(iNaN);
data(cur_index ) = data(cur_index -1);
end
This approach relies on linear indexing to get the previous value. Even if the previous value was NaN, you've overwritten it with a valid value. You are going to run into problems if you need to do something special for the first value in each column. This approach will replace the first value in a column with the last value in the previous column.
  1 件のコメント
Christian Frandsen
Christian Frandsen 2016 年 12 月 6 日
Hi Jim I have generated it manually. The problem is that I also need the first value in each column.

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

カテゴリ

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