Removing NaNs in a struct array

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 件のコメント

Guillaume
Guillaume 2019 年 1 月 15 日
I have a struct array that is 15X1000X1000
With what fields?
There are random NaN's scattered throughout the data in this struct
In any of the fields? Are the fields scalar? arrays (if so what size)?
but the if cannot handle the NaN
if doesn't care one bit about what's in the data, NaN or not. The problem is probably that you don't know how to handle NaN properly.
In any case, it's very unlikely you need loops or if statements.
To know for sure, answer the questions above or better, attach you structure in a mat file.
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 日

0 投票

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.

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

カテゴリ

タグ

質問済み:

2019 年 1 月 15 日

コメント済み:

2019 年 1 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by