It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array of floats Float_t xbins[41] that defines 40 bins i.e. ranges of floats.
E.g. y is in bin 7 if y > xbins[7] && !(y > xbins[8]).
How do I determine what bin a given float should belong to without having 40 if statements?
Please answer in C++ as I don't speak other languages.
If the array is sorted, then do a binary search to locate the correct bin. You'll need a combination of std::sort (if not sorted), then something like std::lower_bound, to locate. You'll need to ensure that operator< is implemented correctly for Float_t.
As it turned out that the bins are not uniformly spaced but have integer bounds, the probably fastest method is to have a (inverse) look up table that apparently has about 100 entries. One needs to make basically two comparisons for the lower & higher bounds.
If the array bounds are derived with a formula, it could be possible to write an inverse formula that outperforms the LUT method.
For a generic case binary search is the way -- and even that can be improved a bit by doing linear interpolation instead of exactly subdividing the range to half. The speed (if the data is not pathological) would be O(loglogn) compared to O(logn) for binary search.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to calculate the mean, median and s.d. of the values inside the vector. I can sort the vector to find out the median but is there an easier way to find the mean and standard deviation rather than adding stuff up?
You can find the median with std::nth_element. Contrary to (apparently) popular belief, this is normally faster than sorting, then finding the middle element -- it's normally O(N) (linear) where sorting is normally O(N log N).
To add the elements for the mean, you can ust std::accumulate, something like:
double total = std::accumulate(std::begin(v), std::end(v), 0.0);
[Note: depending on how old your compiler is, you may need to use v.begin() and v.end() instead of begin(v) andend(v)`). ]
Computing the variance has been covered in a previous question. The standard deviation is simply the square root of the variance.
In order to find the mean, you're simply going to have to add the vector contents up. You can find the median without actually sorting the vector first, but an algorithm for calculating the median on an unsorted vector would almost certainly be much more complex than if it's sorted. Also, I pretty sure that if you calculate the time to find the median on an unsorted vector, it's almost certainly going to be more than the combined time of sorting and extracting the median. (if you're doing it for just the technical challenge, I'll write one for you...)
Since you're probably going to have to sort the vector, you could calculate the mean whilst you're sorting.
EDIT: Didn't see the C++ tag!
If you are using a language that offers functional programming tools, you can foldl the vector with the + function and divide by its length for mean.
For stddev, you can use a lambda : x -> (x - mean)^2 and fold the result with a +.
It's not more computationally efficient, but it probably saves a lot in developer time!
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
To use vector efficiently we need to reserve the memory before setting the elements. But for map and set which are not contiguous containers how we can make them fast and efficient?
I have a vector/set/map of size 10s Millions of doubles and want to add non-repeated elements. I want to make it as fast as possible.
Q1) all STL containers are already as efficient as they can be. It's up to you the programmer to choose what data structures suits the given requirement. You need to understand the pros and cons of each data structures.
Q2) Map[key] = value calls operator[] which can also be used to access elements, not just inserting, whereas insert() function is only specific to inserting. insert() has few other overloading feature not available on operator[], check http://www.cplusplus.com/reference/map/map/insert/
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array which fluctuates about between 0.1429 and 0.1428 it doesn't seem to have a real top or bottom though so those numbers could vary.
if(myarray[N-1]<myarry[N]){/*always happens*/}
if(myarray[N-1]>myarry[N]){/*never happens*/}
the numbers are fractional so there must be smaller fraction in the numbers to show curves on my chart eg: 0.14285216
I am having real trouble with 'greater than' 'smaller than' < > I think it's because i've not got numbers bigger than 1 (myarray[N-1] shows 0 always)
can I do something to my data like increase the range or use another method to '<>'?
really stuck
I'm guessing that what you want to do is display the numbers in an array so as to see the differences between them? The reason for your always/never situation is that the array is sorted, which is probably a good thing. Anyway, to display a greater number of digits, you can use format specifiers, such as
printf ("my ith number: %.10f", myarray[i]);
This will give you myarray[i] with 10 decimal places.
what is:
myarray versus myarry (missing the a between the second r and the y)
Direct from your code given above:
if(myarray[N-1]<myarry[N]){/*always happens*/}
if(myarray[N-1]>myarry[N]){/*never happens*/}
Shouldnt it be:
if(myarray[N-1]<myarray[N]){/*always happens*/}
if(myarray[N-1]>myarray[N]){/*never happens*/}
Also I hope you arent ever using N = 0 as an input to this set of if statements.
You might want to multiply all numbers by 7 and subtract 1 - that will make the relative differences larger.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I tell whether an algorithm is stable or not?..
Also, how does this algorithm Bucketsort compare to Mergesort, Quicksort, Bubblesort, and Insertionsort
?
At first glance it would seem that if your queues are FIFO then it is stable. However I think there some context from class or other homework that would help you make a more solid determination.
From wikipedia:
Stability
Stable sorting algorithms maintain the relative order of records with equal keys. If all keys are different then this distinction is not necessary. But if there are equal keys, then a sorting algorithm is stable if whenever there are two records (let's say R and S) with the same key, and R appears before S in the original list, then R will always appear before S in the sorted list. When equal elements are indistinguishable, such as with integers, or more generally, any data where the entire element is the key, stability is not an issue. However, assume that the following pairs of numbers are to be sorted by their first component:
http://en.wikipedia.org/wiki/Sorting_algorithm#Stability
As far as comparing to other algorithms. Wikipedia has a concise entry on it:
http://en.wikipedia.org/wiki/Bucket_sort#Comparison_with_other_sorting_algorithms
Also: https://stackoverflow.com/a/7341355/1416221
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to write a program that you give an input (number of digits n) and you get as an output a vector of all possible binary grey code generated.
For example,
if n=2, results should be:
V= {00, 01, 10,11}
Actually I am interested in having them bit by bit and not whole integer. Meaning I want to have a 2 D array of integers where I have each word in rows and bits (as int) in cols
Hint: Gray code of num is (num>>1) ^ num. Go through all numbers of the 0..2^N-1 range, and compute their Gray code representation using this simple formula.
EDIT Another hint: the simplest way to convert an integer to binary is using bitset:
bitset<N>((num>>1) ^ num).to_string()
The set V equals { 0, ..., 2^n-1 }. Just compute this set.