Delete rows with NaN records

1 212 views (last 30 days)
Meh on 12 Mar 2012
Commented: Ahmad Alsayed on 22 Nov 2021
How can I delete rows which have NaN on one of the column.

Accepted Answer

Jan on 12 Mar 2012
X = rand(10, 10);
X(X < 0.1) = NaN;
X(any(isnan(X), 2), :) = [];
Or do you mean exactly one NaN in a row?
X(sum(isnan(X), 2) == 1, :) = [];
Jan on 8 Mar 2017
Dear chocho phD: Please post some code which creates your input in proper Matlab syntax. It is tedious to guess the type of your data. While "NA" must be a string, the rest of this thread concerned NaN, which is a double number.
Again: Please open a new thread for a new question. Then please post the complete code. "isempty(strfind(l,'NA'))" is only a hint, but we cannot see, where what is deleted by which command.
A bold guess:
C = {0.255898, 'NA', 0.257966; ...
0.180000, 'RNASE', 0.389999; ...
0.892222, 'NA', 0.458888; ...
0.155523, 'GNAS', 0.892133};
Key = C(:,2);
keep = ~cellfun('isempty', strfind(Key, 'NA')) & ~strcmp(Key, 'NA');
Wanted = C(keep, :)
Now this comment has no relation to my answer or to the original question. You cannot vote or accept this solution. So actually Meh's thread has been "highjacked".

Sign in to comment.

More Answers (2)

Chris Turnes
Chris Turnes on 7 Mar 2017
Not that this question needed another answer, but, you can also check out the rmmissing function that was introduced in R2016b. If you combine this with standardizeMissing, you can convert your 'GNAs' strings to a standard missing indicator, and then remove the rows with rmmissing.

carmen on 12 Mar 2012
check out the isnan() functioion. the following code looks like a workaround but it works:
A=[1 2 3;nan 4 5;nan 6 nan];
B %returns the only nan-free row of A: [1 2 3]
  1 Comment
Ahmad Alsayed
Ahmad Alsayed on 22 Nov 2021
A=[1 2 3;nan 4 5;nan 6 nan]
A = 3×3
1 2 3 NaN 4 5 NaN 6 NaN
B %returns the only nan-free row of A: [1 2 3]
B = 1×3
1 2 3

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!