a = NaN NaN NaN
NaN NaN NaN
4 5 6
7 8 9
i want t remove NaN frm matrix and want to have matrix like belw
a = 4 5 6
7 8 9
how to do it with find functin. i can do like below this but i want using find function
Result_All(isnan(Result_All(:,1)),:) = []

댓글 수: 1

Stephen23
Stephen23 2016년 3월 11일
편집: Stephen23 2016년 3월 11일
See Guillaume's answer for the neatest and fastest way to solve this.

댓글을 달려면 로그인하십시오.

 채택된 답변

KSSV
KSSV 2016년 3월 11일

1 개 추천

a = [NaN NaN NaN
NaN NaN NaN
4 5 6
7 8 9];
k = find(isnan(a)) ;
a(k) = [] ;
reshape(a,[2,3])

댓글 수: 3

Mayank Lakhani
Mayank Lakhani 2016년 3월 11일
thanks for the answer. I want to make it more dynamic s i do not need to use any number like in reshae(a,[2,3]).
KSSV
KSSV 2016년 3월 11일
if every time...you are removing a complete row..it would be easy to get a matrix with same columns as the original. If not getting matrix of same columns or rows of original would be tough.
PS: If it is useful. Accept the answer.
Guillaume
Guillaume 2016년 3월 11일
Note that the find in the above answer is completely unnecessary and only slows down the code. Admittedly the slowdown is negligible in this case.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2016년 3월 11일

1 개 추천

Note that you cannot remove arbitrary nans from a matrix since that would leave 'gaps'. You can only remove entire rows or columns. You can detect rows or columns that are just nan with the all function. It is much better than checking the first column as you have done.
To remove all rows that contain nans:
a = [NaN NaN NaN
NaN NaN NaN
4 5 6
7 8 9];
nanrows = all(isnan(a), 2);
a(nanrows, :) = []
This is the "dynamic" versions. find is completely unnecessary. You can of course add as many unnecessary statements as you want to your code to make slower and more efficient, but why?
a = [NaN NaN NaN
NaN NaN NaN
4 5 6
7 8 9];
nanrows = find(all(isnan(a), 2)); %<- completely unnecessary find as you asked
nanrows = nanrows * 1 + 0; %<- completely unnecessary addition and multiplication, while we're at it
a(nanrows, :) = [];

카테고리

도움말 센터File Exchange에서 NaNs에 대해 자세히 알아보기

태그

질문:

2016년 3월 11일

편집:

2016년 3월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by