Sampling from an array in android kotlin - android

I need an idea on doing this. I'm not good at math.
Maybe it have build in function which i haven't found yet.
I have an array which consists of 2048 data.
I need to get on 250 value out of this.
I'm thinking of
2048/250 = 8.19
which means, I take value on each increment of 8 position in an array.
Is there a function to do this?

Not that I'm aware of, I think the problem is to balance iterations and the randomness of the sampling.
So the naive approach
dataSet.indexedMapNotNull { i, data ->
if (i % 8 == 0) data else null
}
That would run through all the array, so you only need 250 iterations, not dataSet.size iterations. So what about if we iterate 250 times and for each of those we take the 8th times of it
val sample = mutableListOf<DataType>()
for (i in 1..250) {
val positionInDataSet = (i * 8) - 1 //minus one adjust the index for the size
val case = dataSet[positionInDataSet]
sample.add(case)
}
Another alternative would be to simply use copy methods from collections, but the problem is you lose your sampling
dataSet.subArray(0, 250)
Sub-array didn't sample the data in a pseudo-random way but only got the first 250 and that would be biased. The upside is usually array copies methods are a log of N.
Another option would be to randomize things even more by not getting data each 8 but a random position until we hit our desired sample size.
val sample = mutableSetOf<DataType>()
while (sample.size != 250) {
val randomPosition = Random.nextInt(0, dataSet.size)
val randomSelection = dataSet[randomPosition]
sample.add(randomeSelection)
}
Here we use a set, because a Set guarantee unique elements, so you have completely random 250 elements from your data set. The problem with this is that randomness on the position could make the same randomPosition more than once, so you iterate on the data set more than 250 times, this could even be factorial which in larger data sets it would happen and is considered the lowest performance.

Related

How to Count, Compare and store data in Array for Real Time Data coming in micro seconds from sensors using RxJava 2 Android

fun checkT1Value(eventData: EventData?)
{
if(eventData.a == 3.9)
{
if(eventData.received_at - Tbuffer_firstReading!! >= 200000) // This is time Difference condition
{
val alphaValuesArray: DoubleArray = doubleArrayOf(eventData.a) // Also how to store real time data in Array here
}
}
}
(1) I need to Compare values ,do some computation for the data coming in Real time and also store it into Array find the maximum from it.
(2) Also I want to count and display the count in my fragment
1)As I am using RxJava 2.I am using Flowable using onBackPressureBuffer and subscribe it then I get eventData.a
What is the Problem
I am not able to accuarately Compare, Count or Store in array in Real Time data coming in microseconds.
Second problem is my Logs are not coming
I am able to see my Count value on fragment which is giving abnormal values. Not counting properly.(This way I know that it is not working properly )
Also I put toneGenerator inside count . Which triggers abnormally.
What I think of a solution is using Mathematical operators for Observable/Flowable Here I think can provide a solution
Thank you in Advance

I have a few questions about MPAndroidCharts

The data in my graphs use milliseconds and look approximately like this:
[1534928499109,52],[1534928522758,49],[1534928546408,51],[1534928570036,47],[1534928593671,54],
but with many thousand data points. For some reason the points stack on top of each other like in the picture I've attached. How can I fix this? This also happens with HelloCharts.
Points stacking on top of each other.
I prefer MPAndroidChart but HelloCharts got this awesome view, previewChart. Here's an example: https://github.com/lecho/hellocharts-android. Does MPAndroidCharts support previewCharts or something similar?
I am currently using a valueformatter to change milliseconds to date. Can I somehow get the difference between the smallest and biggest currently visible value and this way dynamically change the valueformatter to format more specific time?
Thanks in advance for any answers!
Only answering 3.:
chart.visibleXRange gives you the difference between the lowest and the highest visible x value. Similarly, chart.visibleYRange gives the values for the Y axis.
Be aware that (if you have defined a dragOffsetX) when scrolled all the way to the left or the right border of the chart, then the lowest or the highest value, respectively, is the lowest/highest value actually occurring in your data, but not the x value corresponding to the left/right border of the chart. To get that value, you can use chart.getValuesByTouchPoint(...) and chart.contentRect.
I use the following function to determine the exact interval between labels which helps me decide in what granularity I want to format the labels (in my case seconds vs milliseconds). The main part which transforms the rawInterval into interval is taken from com.github.mikephil.charting.renderer.AxisRenderer.computeAxisValue and translated to Kotlin:
fun calculateIntervalBetweenLabels(): Double {
val range = chart.getValuesByTouchPoint(chart.contentRect.right, 0f, YAxis.AxisDependency.LEFT).x - chart.getValuesByTouchPoint(chart.contentRect.left, 0f, YAxis.AxisDependency.LEFT).x
val rawInterval = range / chart.xAxis.labelCount
var interval = Utils.roundToNextSignificant(rawInterval).toDouble()
val intervalMagnitude = Utils.roundToNextSignificant(10.0.pow(log10(interval).toInt())).toDouble()
val intervalSigDigit = (interval / intervalMagnitude).toInt()
if (intervalSigDigit > 5) {
interval = floor(10 * intervalMagnitude)
}
return interval
}
In simpler cases without dragOffsetX, the first line could be replaced by val range = chart.visibleXRange.
In my ValueFormatter I do this:
override fun getFormattedValue(value: Float): String {
return when {
calculateIntervalBetweenLabels().roundToLong() >= 1000 -> formatValueInSeconds(value)
else -> formatValueInMilliseconds(value)
}
}
I've figured a few things out. In case anyone comes across this in the future and wonders the same thing.
MPAndroidCharts class Entry uses Float. Max value for Float is 2^23 and everything above that is rounded, the points get the same x-value. I fix this by subtracting 1.5 billion from every value and dividing by 100. Then in the ValueFormatter, I undo this.
I don't know, yet.
My solution was to calculate the difference between every value that gets formatted in the ValueFormatter. If the difference is less than zero, the formatter has looped around and that value is the displayed interval. Another solution suggested using chart.visibleXRange, which is much simpler.

Maintaining dynamically computed values in firebase

I have a simple firebase database: /rides is a list of simple objects like this
{
car: "Toyota"
minutes: 15
}
and I need to display sum of minutes of all the rides. The obvious solution is to load all the rides and calculate the sum. But if I have several hundreds of rides this is very slow, up to several seconds.
So it seems I have to maintain a separate field /totalMinutesin the database for this. But thus I will have to manually update /totalMinutes every time I add/remove/change a ride. Anyway this is not a big deal of work.
But what if I need to calculate total minutes only for a subset of rides? For instance only for "Toyota" cars or "Ford" cars? Manual maintaining /totalMinutesFord, /totalMinutesToyota now doesn't seem so easy.
So what is the correct way to maintain such dynamic values in firebase?
Firebase has no way to get automatically calculate values based on the data in your database.
So your two options are:
calculate the value whenever you update the data
retrieve all the data and calculate the value on the client
You already (wisely) decided that retrieving all data is not a good idea. Your users will be grateful for that.
So that leaves calculating the derived values whenever you update the data of a ride. I'm not sure why doing that for multiple values would be more difficult than doing it for a single value. It may be more code, but it's pretty much the same code:
var ride = { car: "Toyota", minutes: 15 };
ref = new Firebase('https://yours.firebaseio.com/');
ref.child('rides').push(ride);
ref.child('totalMinutes').transaction(function(current_value) {
return (current_value || 0) + ride.minutes;
});
ref.child('totalMinutes'+ride.car).transaction(function(current_value) {
return (current_value || 0) + ride.minutes;
})

How to store a gps coordinates in mysql, and then retrieve it without time lag?

I have completed a phonegap app which pools gps cordinate data every 10 sec to the server. now according to the calculations 8 hours of tracking it will store around 8*60*6=2880 records per user. my current requirement is limited to use of 20 user. (basically it tracks a users rout travelled)
There are two parts to the question:
what is the best way to store the data and retrieve it as fast as possible.
is it possible to display 2880 coordinates at a time on google maps API v3 ? if not, what is the best way to display the rout traveled?
I am having good results with 90 or so points, for one of my demos, but the enormous 2880 records per user per 8 hours is what worries me.
Thanks
EDIT 1
Although this is an old question , I recently worked on a project where I displayed about 10K points on the map, I hope my observations would help the future visitors:
The google maps as if now do not have a hard limit on the number of points you can display on the client side.
The number of points you can display on the client side is entirely dependent on the client side `Hardware` , bigger the number of points using a jpeg or gif marker , slower will be the renders , when moving around or zooming in and out
To have huge number of pointers on the map with a minimal performance hit, precomputing the number of points needed to be rendered before and after pan or zoom occurs will help a lot.
So here is a possible solution:
First of all, you need to find out how many points Google Maps API can handle and still show the line. I think this will just take some testing or researching. Anyways once you find your magical number of points to display to plot your path then take that number and multiply it by 2/3.
For instance if a good path needs have say 90 points then calculate 90*2/3
The reason for 2/3 is that the following loop will return a max number of points that is averagely equal to 3/2 times the variable we use so using. 60 would give us on average 90 plots. There is a case where the most returned plots would be (2 * (magical number of points)) - 1 for instance say we want on average of 90 points then we could in some rare cases have (2*(90*2/3))-1 = 119 points You will just have to do some testing after implementation to make sure that your magical number of points works good for maps with 2/3 of the magical number of points and 2 * magical number of points -1. I hope this isn't too confusing... I tried to explain as best I can.
The rest of this is going to be sudo code. You will have to adapt it for whatever language you connect to MySQL with:
//get the total number of rows returned
var total_rows = mysql->num_rows;
//calculate max to be 2/3 times your magic number for max plots, i.e. 90
var max_plots = 90*2/3;
//define empty plots array to fill with coordinates
var plots = array();
//check if total_rows is less than max_plots then:
if(total_rows > max_plots){
//find the quotient of the the divident total_rows and the divisor max_plots rounded down to the nearest whole int
var quotient = floor(total_rows/max_plots);
//define variable i to use in loop
var i = 1;
//loop through returned rows
while(row = mysql->fetch_row()){
//return only rows that are the first, last, or are dividable by the quotient evenly; Note: if your language supports it, use the Modulus operator like (i % quotient) == 0 for the last or statement.
if(i == 1 || 1 == total_rows || (i - (i * (floor(i/quotient)))) == 0){
//set plots to use on map
plots[] = array(
'lat' => row['lat'],
'lon' => row['lon'],
);
}
//increment counting variable
i++;
}
// else if total_rows less than or equal to max_plots retrieve all plots
} else {
while(row = mysql->fetch_row()){
plots[] = array(
'lat' => row['lat'],
'lon' => row['lon'],
);
}
}
This may not be the best way as it still requires to retrieve all of the rows from the database, but it does solve how to only print a selected maximum amount evenly spaced on the Google map.
Note: Be sure that your query orders the rows by an auto incrementing key or some other way so that the plots will be in order that they were entered into the database.
The most detailed maps would be a map with (2 * magic_plot_number) - 1 and your least details map would contain magic_plot_number or if lower, the number of total_plots. That being said an 8 hour tracking would plot a path with points every 7 minutes and 51 seconds totaling 61 points over 8 hours using the magic plot number of 90. The more plots the closer number of points will be to 2/3 * the magic plot number
I hope this helps you with this situation.

ANDROID How to reduce String allocations

I've managed to get my allocations down to next to nothing using DDMS (great tool), this has drastically reduced my GCs to about 1 or 2 every 3 minutes. Still, I'm not happy because those usually cause a noticeable delay in the game (on some phones) when you interact with it.
Using DDMS, I know what the allocations are, they are Strings being converted from integers used to display game information to the HUD.
I'm basically doing this:
int playerScore = 20929;
String playerScoreText = Integer.toString(playerScore);
canvas.drawText(playerScoreText, xPos, yPos);
This happens once each frame update and the HUD system is modular so I plug things in when I need and this can cause 4 or 5 hud elements to allocate Strings and AbstractStringBuilders in DDMS.
Any way to reduce these further or eliminate all the String allocations and just reuse a String object?
Thanks,
Albert Pucciani
Reading your question reminded me of one of Robert Greens articles that I read quite some time ago. It discusses your problem almost identically. http://www.rbgrn.net/content/290-light-racer-20-days-32-33-getting-great-game-performance . Skip down to day 33 and start reading.
Remember the last int score and its string representation. On a new frame check if the score is the same. If the same, then no need to create a new string - just use the old one.
Here's what I've done in the past. This will eliminate string allocations.
I create a char[] of a size that will be at least as large as the maximum number of characters you will need to display on the screen. This means that you should select a maximum high score that is achievable in the game. The way you have it now let's you display a score as high as 2^31-1 which is insanely huge, it's not practical with respect to the game. Keep in mind, this is your game, so it's ok to limit the max score to something more reasonable in the context of the game. Pick a number that will virtually be impossible to achieve. Setting this limit will then set you up to be able to not have to muck around with converting large integers to String objects.
Here's what's required:
First, you need to be able to separate the digits in an integer and convert them to char without creating String objects. Let's say you want to convert the integer of 324 into three separate characters '3','2','4' to be placed in the text char[]. One way you can do this is by taking the value 324 and do a mod 10 to get the lowest digit. So 324%10 = 4. Then divide the value by ten and do another mod 10 to get the next digit. So (324/10)%10 = 2, and (324/100)%10 = 3.
int score = 324;
int firstPlaceInt = score%10; // firstPlace will equal 4
int tensPlaceInt = (score/10)%10; // tensPlace will equal 2
int hundresPlaceInt = (score/100)%10; // hundredsPlace will equal 3
You will have to do the above in a loop, but this expresses the idea of what you're trying to do here.
Next, with these digits you can then convert them to chars by referencing a character map. One way to do this is you can create this character map by making a char[] of size 10 and placing values 0 - 9 in indexes 0 - 9.
char[] charMap = {'0','1','2','3','4','5','6','7','8','9',};
So doing this:
int score = 324;
char firstPlace = charMap[score%10];
char tenslace = charMap[(score/10)%10];
char hundredsPlace = charMap[(score/100)%10];
Will create the chars you need for the 3 digits in score.
Now, after all that, I would limit the highest score to say 99,999 (or whatever makes sense in your game). This means the largest "string" I would need to display is "Score: xx,xxx". This would require a char[] (call it text for this example) of size 13. Initialize the first 7 characters with "Score: ", these will never need to change.
char[] text = new char[13];
text[0] = 'S';
text[1] = 'c';
text[2] = 'o';
text[3] = 'r';
text[4] = 'e';
text[5] = ':';
text[6] = ' ';
The next 6 will vary based on the score. Note, that you may not necessarily fill in all 6 of those remaining characters, therefore you need to create an int (call it scoreCount for this example) which will tell you how many characters in the text char[] are actually relevant to the current score in the game. Let's say I need to display "Score: 324", this only takes 10 chars out of the 13. Write the 3 chars for the score of 324 into char[7] to char[9], and set scoreCount to 10 to indicate the number of valid characters in the char[].
int scoreCount = 7;
text[9] = charMap[score%10]; // This is firstPlace
text[8] = charMap[(score/10)%10]; // This is tensPlace
text[7] = charMap[(score/100)%10]; // This is hundredsPlace
scoreCount = 10;
You will probably have to do the above in a loop, but this should express the general idea of what you're trying to do here.
After that, you can just use drawText (char[] text, int index, int count, float x, float y, Paint paint). index will be 0, and count will be scoreCount which indicates how many characters in text should be drawn. In the example above, it doens't matter what's in text[10] to text[12], it's considered invalid. You can continue to update text[] using the character map, and this should not create any objects.
I hope this helps. The code above isn't very robust, but I wrote it out as more of an expression of the ideas I'm trying to convey. You will have to create your own loops and manage the data properly within your code, but this sums up the mechanics of what needs to happen to avoid the use of Strings/StringBuilder/StringBuffer/etc.

Categories

Resources