0

I will use the data from accelerometer inside the phone and calculate angles from it. The thought is to store the angle data in an array using core data or write it into a file.

But I still need to do some calculation, for example the median and the 90 percentile number . How could I do it? Should I store the whole data first and then conduct calculation, like sorting from smallest to largest and then count?

I couldn't think of a good way to do it. If anyone has some suggestion I would be really grateful!

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
Lynn_Yang
  • 157
  • 1
  • 11
  • possible duplicate of [ios most efficient way to get average value while also filtering out some objects](http://stackoverflow.com/questions/8142435/ios-most-efficient-way-to-get-average-value-while-also-filtering-out-some-object) – Léo Natan Mar 02 '15 at 19:47
  • The solution in the comment above uses NSExpression, which does have the `median:` function. But for the percentiles I think you will have to calculate them manually, sorting then counting as you suggest. But remember you can use `fetchOffset` and `fetchBatchSize` to avoid reading all the rows into memory. – pbasdf Mar 03 '15 at 08:37
  • I don't understand why do I need to use `fetchOffset`? What do you mean to avoid reading into memory? Now I used `sortDescriptor` and I can get the 90 percentile by use `ObjectAtIndex`. It seems to work well! – Lynn_Yang Mar 04 '15 at 15:29
  • @Mushroomcloud Sorry for delay in replying - you don't **need** to use `fetchOffset`, but you **can** use it to avoid loading all the angles into memory. Rather than "fetch everything, sort it and then find the 90 percentile point", you could effectively "fetch the one (or two, to interpolate) values at the 90th percentile point". But if memory is not a problem, then stick with `objectAtIndex`. – pbasdf Mar 27 '15 at 14:03

0 Answers0