How can I remove rows containing Nan values in table?

42 visualizaciones (últimos 30 días)
Behrooz Daneshian
Behrooz Daneshian el 16 de En. de 2023
Comentada: Image Analyst el 17 de En. de 2023
Hi all,
I have attached a table I am working one. In some rows of a table, there is NaN values both for TAVG and Tfreezing. How can I remove these rows? I tried using find(table.TAVG==NaN) but I do not know why it is not working. Can anyone help me in this regard?

Respuestas (2)

dpb el 16 de En. de 2023
ixnan=(isnan(tTable.TAVG)|isnan(tTable.Tfreezing)); % logical vector either variable nan
tTable(ixnan,:)=[]; % remove those rows
alternatively, you can keep the finite rows...
tTable=tTable(~ixnan,:); % neither is nan
"I tried using find(table.TAVG==NaN)..."
Do NOT use "table" as the name of a variable -- that aliases the MATLAB table function; a very bad idea.
I'd suggest something a little more meaningful to the content of the table for your variable name, I just used tTable above as a fill-in for your table variable to avoid repeating the above aliasing.

Image Analyst
Image Analyst el 16 de En. de 2023
Editada: Image Analyst el 16 de En. de 2023
Try this:
% Load .mat file.
s = load('behrooz Table.mat')
% Extract variable from structure into table.
t = s.stations_1__3__3__8_
% Extract columns 2 and 3 into a numerical array.
m = table2array(t(:, 2:3));
% See which rows have a nan in them.
nanRows = any(isnan(m), 2);
% Delete those rows with nans in column 2 or 3
% In other words, extract only rows that don't have a nan in them into a
% new variable. You could use the same variable as the original if you want.
tNoNans = t(~nanRows, :)
Or, assuming you already have table t in memory, and want to do it all in one line
tNoNans2 = t(~any(isnan(t{:, 2:3}), 2), :);
though it's a bit cryptic and not commented at all like the first example so it might be harder to figure out what it's doing later.
  2 comentarios
Image Analyst
Image Analyst el 17 de En. de 2023
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.

Iniciar sesión para comentar.


Más información sobre Matrix Indexing en Help Center y File Exchange.


Community Treasure Hunt

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

Start Hunting!

Translated by