フィルターのクリア

Cleaning Matrix

2 ビュー (過去 30 日間)
Xavier
Xavier 2011 年 9 月 13 日
Hi,
I have the following matrix called Data(size=n*1) (actually tenth of them) that I need to clean. The idea is simple:
If there is a number then no change
If there is not a number then, I replace by:
the previous number, if there is one.
the next number, if there is no previous number.
Example Data matrix
Data=
[]
[]
20
[]
25
[]
100
Result=
20
20
20
20
25
25
100
Your help would be much appreiated.
Thanks
Xavier

採用された回答

Andrei Bobrov
Andrei Bobrov 2011 年 9 月 13 日
Data={[]
[]
20
[]
25
[]
100};
t = ~cellfun(@isempty,Data)
Data(1) = Data(find(t,1,'first'))
t = [true;t(2:end)]
idx = cumsum(t)
D1 = Data(t)
out = D1(idx)
  1 件のコメント
Xavier
Xavier 2011 年 9 月 13 日
It works nicely. Thanks

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 9 月 13 日
It is not possible to construct such matrices in MATLAB.
Are you using cell arrays? If so then if the empty matrix shows up then should only the first element of the previous / next number be copied, or should it be full cell entry that is copied?
Or where you show [] in your example, is that really NaN "not a number" ?
Does NaN ("not a number") count as being a number for your purposes? E.g., if you had [] nan 20 then should that become nan nan 20 or should it become 20 20 20 ?

カテゴリ

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