-1

I am using the swift accelerate APIs to process some data. However a few of these data points contain NaN values (result of trying to divide a zero value).

My question is can I easily deal with this using any inbuilt swift function? I am looking at replacing all values of NaN with zero values so that I can still further process these large data sets without an error.

samp17
  • 547
  • 1
  • 4
  • 16

1 Answers1

3

You can use the map method to transform all NaN values to 0 and all other values to themselves:

let arrayWithoutNaNs = yourArray.map { $0.isNaN ? 0 : $0 }

Since your arrays are long, you might also want consider doing this lazily by adding a .lazy before .map.

Sweeper
  • 213,210
  • 22
  • 193
  • 313