patterncsharpMinor
Sorting numbers into bins
Viewed 0 times
sortingnumbersintobins
Problem
I have two 2D arrays of doubles
The list
I need to go through each of the numbers in
a and b. Both have two rows and different numbers of columns. a [[ 1,2,3][4,5,6]]The list
b represents a number of bins of numbers and the list is a list of numbers I'm trying to sort into these bins. I need to go through each of the numbers in
b to look for its corresponding bin. Here's the code I have, which works perfectly but I need to optimize it. Any ideas? int k = 0;
for (int i = 0; i (b[0, j] - binSize / 2)) {
c[0, k] = a[0, i];
c[0, k] = b[0, j];
c[1, k] = a[1, i];
c[1, k] = b[1, j];
k++;
}
if (k >= a.GetLength(1)) {
break;
}
}
}Solution
Here's the code I have, which works perfectly
It is obviously better to extract the
Also
As you overwrite the values of
this can be reduced to
As the context isn't known let's keep the check for
the check for
but we place it to the looping condition
and we should refactor a[0,i] to outside the inner loop
which results overall in
int k = 0;
for (int i = 0; i (b[0, j] - binSize / 2)) {
c[0, k] = a[0, i];
c[0, k] = b[0, j];
c[1, k] = a[1, i];
c[1, k] = b[1, j];
k++;
}
if (k >= a.GetLength(1)) {
break;
}
}
}It is obviously better to extract the
binSize / 2 outside of the for loops, as binSize is never changed. So the repeating binSize / 2 isn't needed. Assuming binSize is an int: int halfBinSize = binSize / 2;Also
a.GetLength(1) and b.GetLength(1) won't change, but are queried each time in the inner loop so refactor like this:int lengthA = a.GetLength(1);
int lengthB = b.GetLength(1);As you overwrite the values of
c[0, k] = a[0, i];
c[0, k] = b[0, j];
c[1, k] = a[1, i];
c[1, k] = b[1, j];this can be reduced to
c[0, k] = b[0, j];
c[1, k] = b[1, j];As the context isn't known let's keep the check for
k >= a.GetLength(1) the check for
k >= a.GetLength(1) can be removed as you are using the a[,] only to compare with b[,] the check can be removed completely but we place it to the looping condition
for (int j = 0; k >= lengthA && j < lengthB; j++)and we should refactor a[0,i] to outside the inner loop
double currentA = a[0,i];which results overall in
int halfBinSize = binSize / 2;
int lengthA = a.GetLength(1);
int lengthB = b.GetLength(1);
double currentA;
int k = 0;
for (int i = 0; i = lengthA && j (b[0, j] - halfBinSize))
{
c[0, k] = b[0, j];
c[1, k] = b[1, j];
k++;
}
}
}Code Snippets
int k = 0;
for (int i = 0; i < a.GetLength(1); i++) {
for (int j = 0; j < b.GetLength(1); j++) {
//if fragment falls in bin, add it to the new object
if (a[0, i] <= (b[0, j] + binSize / 2) &&
a[0, i] > (b[0, j] - binSize / 2)) {
c[0, k] = a[0, i];
c[0, k] = b[0, j];
c[1, k] = a[1, i];
c[1, k] = b[1, j];
k++;
}
if (k >= a.GetLength(1)) {
break;
}
}
}int halfBinSize = binSize / 2;int lengthA = a.GetLength(1);
int lengthB = b.GetLength(1);c[0, k] = a[0, i];
c[0, k] = b[0, j];
c[1, k] = a[1, i];
c[1, k] = b[1, j];c[0, k] = b[0, j];
c[1, k] = b[1, j];Context
StackExchange Code Review Q#60463, answer score: 7
Revisions (0)
No revisions yet.