フィルターのクリア

Remove Inf and NaN values in a Point Cloud in the fastest way

163 ビュー (過去 30 日間)
Meghana Dinesh
Meghana Dinesh 2014 年 11 月 20 日
回答済み: Vignesh Kumar 2022 年 8 月 11 日
If I have a matrix A
A = [1, 11, 21; NaN,12, 22; 3, 13, Inf; NaN,14, NaN; 5, Inf, NaN; 6, 16, 26];
I want to eliminate all rows which have either Inf or NaN elements.
So the expected result would be : [1, 11, 21; 6, 16, 26];
Since I will be working with images of dimensions 4000 X 3000, I want a very fast and efficient way of doing this.
Thank you =)

採用された回答

Adam
Adam 2014 年 11 月 20 日
A = A( ~any( isnan( A ) | isinf( A ), 2 ),: )
  3 件のコメント
Adam
Adam 2014 年 11 月 21 日
編集済み: Adam 2014 年 11 月 21 日
any( A > 0 & A < 100, 2 )
will give you the indices of all rows satisfying 0 < A < 100. So
A = A( ~any( A > 0 & A < 100, 2 ), : );
will remove those rows from A.
I often prefer to do things like that in the two lines for better readability, so:
idx = any( A > 0 & A < 100, 2 );
A = A( ~idx );
That way you can also easily double-check the intermediate indexing result before it deletes the wrong rows of your matrix!
Your version was lacking a '(' somewhere, but also the ,2 argument is for the 'any' function, telling it to run on the 2nd dimension. To be honest I always get mixed up with rows and columns and dimension 1 and 2 so I just try them out until I get the right one!
Meghana Dinesh
Meghana Dinesh 2014 年 11 月 22 日
Thank you =) I couldn't find all this explanation on tutorials. Learning to use these functions comes from experience :D

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

その他の回答 (1 件)

Vignesh Kumar
Vignesh Kumar 2022 年 8 月 11 日
%%Try this. It worked for me
A = [1, 11, 21; NaN,12, 22; 3, 13, Inf; NaN,14, NaN; 5, Inf, NaN; 6, 16, 26];
Anew=A((isfinite(A)))
Anew = 12×1
1 3 5 6 11 12 13 14 16 21

カテゴリ

Help Center および File ExchangePoint Cloud Processing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by