Remove rows that contain NaN

5 ビュー (過去 30 日間)
Christopher Wible
Christopher Wible 2019 年 6 月 19 日
コメント済み: Rik 2019 年 6 月 19 日
I am currently trying to remove all rows that contain a NaN.
In other words, if you are given this input : A = [ 1 5 8; -3 NaN 14; 0 6 NaN]
The output should be: B = [1 5 8]
This is what I have so far, but I keep getting an error saying the ii index cannot exceed 1.
A = [ 1 5 8
-3 NaN 14
0 6 NaN ];
B = remove_nan_rows(A)
function B = remove_nan_rows(A)
[row,column] = size(A);
for ii = 1:row
for jj = 1:column
if isnan(A(ii,jj)) == 1
A(ii,:) = [];
end
end
end
B = A;
end

回答 (1 件)

madhan ravi
madhan ravi 2019 年 6 月 19 日
B=A;
B(any(isnan(B),2),:)=[]
  7 件のコメント
Adam Danz
Adam Danz 2019 年 6 月 19 日
Yeah, good catch. Also, there are some circumstances where you can remove an element from a matrix such as
a = 1:10;
a(2) = [];
so i'll remove that comment to avoid confusion.
Rik
Rik 2019 年 6 月 19 日
You can also loop backwards
for n=size(A,2):-1:1

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by