How to find column and row indexes of items that are +-inf or Nan ?

조회 수: 2 (최근 30일)
tci27
tci27 2013년 7월 18일
답변: rui gao 2018년 11월 25일
Hello
I have a very large matrix X where some elements can be -+inf or Nan. Currently I loop over all elements, check each one and handle it. It is taking forever. How can I easily find the [row,col] coordinates of such items ? I tried fiddling with isfinite(X) but in vain
Thanks much for any help

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 7월 18일
편집: Azzi Abdelmalek 2013년 7월 18일
A=[inf 1 4 nan;4 -inf 2 nan]; % Example
[ii,jj]=find(isnan(A)| isinf(A))
You can also use logical indexing, which is faster
idx=isnan(A)| isinf(A)
  댓글 수: 1
tci27
tci27 2013년 7월 18일
Thanks much, got it, working some 1000 times faster than before :)

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

추가 답변 (2개)

Jos (10584)
Jos (10584) 2013년 7월 18일
편집: Jan 2013년 7월 18일
Using ISFINITE is fine. You just have to negate the outcome
Here is a small example, showing all the steps:
M = [1 Inf 3 ; 11 12 NaN]
tf = isfinite(M)
tf = ~tf
[ri,ci] = find(tf)
which you can combine into a single line if you understand each step
[ri,ci] = find(~isfinite(M))

rui gao
rui gao 2018년 11월 25일
Hi. If I need omit these elements (inf) to calculate the variance, how to achieve it?

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by