Removing NaNs in a struct array

11 次查看(过去 30 天)
John Kelley
John Kelley 2019-1-15
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