Borrar filtros
Borrar filtros

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

21 visualizaciones (últimos 30 días)
ahmed obaid
ahmed obaid el 16 de En. de 2017
Comentada: ahmed obaid el 18 de En. de 2017
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 comentarios
Image Analyst
Image Analyst el 16 de En. de 2017
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 el 18 de En. de 2017
ok, thanks for your interesting attached a portion data in an excel file

Iniciar sesión para comentar.

Respuestas (2)

dpb
dpb el 16 de En. de 2017
doc isnan
doc isempty
doc all
Look for "logical addressing"

Niels
Niels el 16 de En. de 2017
Editada: Niels el 16 de En. de 2017
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 comentarios
Guillaume
Guillaume el 16 de En. de 2017
@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

Iniciar sesión para comentar.

Categorías

Más información sobre Data Import from MATLAB en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by