Removing NaNs in a struct array

조회 수: 18 (최근 30일)
John Kelley
John Kelley 2019년 1월 15일
댓글: madhan ravi 2019년 1월 16일
I have a struct array that is 15X1000X1000. There are random NaN's scattered throughout the data in this struct and I would like to replace these with random values between 1 and 10. I have tried writng an if statement to do it but the if cannot handle the NaN. How should i approach this.
  댓글 수: 3
John Kelley
John Kelley 2019년 1월 15일
It is actully a 15x1000x1000 matrix not a struct array. Would the approach be differnet for the matrix
madhan ravi
madhan ravi 2019년 1월 16일
Do you want to remove nan rows or columns or pages??

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

답변 (1개)

Stephan
Stephan 2019년 1월 15일
편집: Stephan 2019년 1월 15일
Hi,
for a matrix A in R2018b use:
A(isnan(A)) = randi(10,1,sum(isnan(A),'all'))
Best regards
Stephan
  댓글 수: 1
Guillaume
Guillaume 2019년 1월 15일
Instead of sum(isnan(A), 'all') you could use nnz(isnan(A)) which is shorter and works in any version of matlab. It's possibly faster as well.

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

카테고리

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