How can I remove rows containing Nan values in table?

33 Ansichten (letzte 30 Tage)
Behrooz Daneshian
Behrooz Daneshian am 16 Jan. 2023
Kommentiert: Image Analyst am 17 Jan. 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?

Antworten (2)

dpb
dpb am 16 Jan. 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 am 16 Jan. 2023
Bearbeitet: Image Analyst am 16 Jan. 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 Kommentare
Behrooz Daneshian
Behrooz Daneshian am 17 Jan. 2023
Thank you so much. it is the right way to do that.
Image Analyst
Image Analyst am 17 Jan. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by