How can I remove rows containing Nan values in table?

39 次查看(过去 30 天)
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?

回答(2 个)

dpb
dpb 2023-1-16
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 2023-1-16
编辑:Image Analyst 2023-1-16
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 个评论
Image Analyst
Image Analyst 2023-1-17
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by