How do we replacing empty or (NaN) cells with 0's in Matlab?

23 ビュー (過去 30 日間)
ahmed obaid
ahmed obaid 2017 年 1 月 16 日
コメント済み: ahmed obaid 2017 年 1 月 18 日
Dear experiences
i have a dataset stored in an excel file where some cells doesn't contain any values, therefore when importing or reading this data and stored it either in table or matrix .. empty cells either places in it NaN or still contain nill values .. so
first: i would like to replacing [] or NaN cells by 0's is there any code or function can do that please, i would thanks any one can give me a solution for this issue, for example when use :
data=xlread(filename), data will be view as follow
second: when importing this data using read table function, some columns are fully empty is there any function can be used to delete columns or rows if its never contain any values.. as shown in 2nd figure ..
thanks for any suggestion
---- table----
  2 件のコメント
Image Analyst
Image Analyst 2017 年 1 月 16 日
State what kind of data you'd prefer: A numerical matrix, or a table. And attach your workbook so we can try things.
ahmed obaid
ahmed obaid 2017 年 1 月 18 日
ok, thanks for your interesting attached a portion data in an excel file

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

回答 (2 件)

dpb
dpb 2017 年 1 月 16 日
doc isnan
doc isempty
doc all
Look for "logical addressing"

Niels
Niels 2017 年 1 月 16 日
編集済み: Niels 2017 年 1 月 16 日
A(isnan(A))=0 % turns every NaN to zeros
to "delete" a row set the row =[]
if A(i,:)==0
A(i,:)=[]
you could use a for loope, but if you do,start from the last row
  2 件のコメント
Guillaume
Guillaume 2017 年 1 月 16 日
@Niels, never ever recommend to beginners the syntax
if somevector
%do something
end
Always wrap the vector in any or all depending on what is desired. There are plenty of questions on Answer which are due to the person not knowing what if does when passed a vector. In the above case:
if all(A(i, :) == 0)
A(i, :) = [];
end
And actually, do you know, without looking up the doc or testing in matlab what the output of the following is?
if [true false]
disp('test 1 passed');
end
if [true true]
disp('test 2 passed');
end
if [false false]
disp('test 3 passed');
end
if [true true true true true true false true true]
disp('test 4 passed');
end
Niels
Niels 2017 年 1 月 16 日
y i know

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

カテゴリ

Help Center および File ExchangeData Import from MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by