How can I remove rows containing Nan values in table?
조회 수: 24 (최근 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?
댓글 수: 0
답변 (2개)
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.
댓글 수: 0
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
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 Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!