简体   繁体   中英

How to deal with NaN in large arrays using swift

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM