min

Most efficient way to find smallest of 3 numbers Java?

我与影子孤独终老i 提交于 2019-11-28 21:04:54
I have an algorithm written in Java that I would like to make more efficient. A part that I think could be made more efficient is finding the smallest of 3 numbers. Currently I'm using the Math.min method as below: double smallest = Math.min(a, Math.min(b, c)); How efficient is this? Would it be more efficient to replace with if statements like below: double smallest; if (a <= b && a <= c) { smallest = a; } else if (b <= c && b <= a) { smallest = b; } else { smallest = c; } Or if any other way is more efficient I'm wondering if it is worth changing what I'm currently using? Any speed increase

How can I find the maximum or minimum of a multi-dimensional matrix in MATLAB? [duplicate]

你离开我真会死。 提交于 2019-11-28 20:51:05
This question already has an answer here: Index of max and min value in an array 3 answers I have a 4D array of measurements in MATLAB. Each dimension represents a different parameter for the measurement. I want to find the maximum and minimum value and the index (i.e. which parameter) of each. What's the best way to do it? I figure I can take the max of the max of the max in each dimension, but that seems like a kludge. Adrien Quick example: %# random 4 d array with different size in each dim A = rand([3,3,3,5]); %# finds the max of A and its position, when A is viewed as a 1D array [max_val,

Is there an easy way to make a min heap in C++?

拟墨画扇 提交于 2019-11-28 19:26:40
I'm very new to C++, and I was wondering if there was a way to make a min heap in C++ from the standard library. Use make_heap() and friends, defined in <algorithm> , or use priority_queue , defined in <queue> . The priority_queue uses make_heap and friends underneath. #include <queue> // functional,iostream,ctime,cstdlib using namespace std; int main(int argc, char* argv[]) { srand(time(0)); priority_queue<int,vector<int>,greater<int> > q; for( int i = 0; i != 10; ++i ) q.push(rand()%10); cout << "Min-heap, popped one by one: "; while( ! q.empty() ) { cout << q.top() << ' '; // 0 3 3 3 4 5 5

MySQL: Select N rows, but with only unique values in one column

混江龙づ霸主 提交于 2019-11-28 16:48:06
问题 Given this data set: ID Name City Birthyear 1 Egon Spengler New York 1957 2 Mac Taylor New York 1955 3 Sarah Connor Los Angeles 1959 4 Jean-Luc Picard La Barre 2305 5 Ellen Ripley Nostromo 2092 6 James T. Kirk Riverside 2233 7 Henry Jones Chicago 1899 I need to find the 3 oldest persons, but only one of every city. If it would just be the three oldest, it would be... Henry Jones / Chicago Mac Taylor / New York Egon Spengler / New York However since both Egon Spengler and Mac Taylor are

How can I get the max (or min) value in a vector?

元气小坏坏 提交于 2019-11-28 16:02:16
How can I get the max (or min) value in a vector in C++ ? I have seen a few solutions for this on Google but none of them made sense to me :( Can someone explain in an easy straightforward noob way how to get the max or min value from a vector please? and am I wrong in assuming it would be more or less the same with an array? I need an iterator right? I tried it with max_element but kept getting an error? vector<int>::const_iterator it; it = max_element(cloud.begin(), cloud.end()); error: request for member ‘begin’ in ‘cloud’, which is of non-class type ‘int [10]’ EDIT: I was not able to

Using std::max_element on a vector<double>

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 15:01:32
问题 I'm trying to use std::min_element and std::max_element to return the min and max elements in a vector of doubles. My compiler doesn't like how I'm currently trying to use them, and I don't understand the error message. I could of course write my own procedure to find the min/max, but I'd like to understand how to use the functions. #include <vector> #include <algorithm> using namespace std; int main(int argc, char** argv) { double cLower, cUpper; vector<double> C; // code to insert values in

Get object with minimum value using extension method min()

夙愿已清 提交于 2019-11-28 13:56:04
list.Min() gets me the min value as integer. I want to get the object in List which has the min value for a certain property X. How can I do that? Jon Skeet Have a look at MinBy in MoreLINQ - or I believe Reactive Extensions has something similar in System.Interactive : var cheapestProduct = products.MinBy(p => p.Price); If more than one item has the lowest value, the earliest one in the sequence will be returned. 来源: https://stackoverflow.com/questions/6414052/get-object-with-minimum-value-using-extension-method-min

Does MINLOC work for arrays beginning at index 0? (Fortran 90/95)

巧了我就是萌 提交于 2019-11-28 12:49:13
After using C for a while, I went back to Fortran and allocated the arrays in my code from index 0 to N: real(kind=dp), dimension(:), allocatable :: a allocate(a(0:50)) I needed to find the index of the minimum absolute value of the array, so I used MINLOC, and to check this I compared it to MINVAL: minloc(abs(a(:))) minval(abs(a)) The result of MINLOC was index 42 but the result of MINVAL corresponded to 41 . Here is the relevant section from the output: Index i a(i) 39 0.04667 40 0.02222 41 0.00222 !This was clearly the minimum value 42 0.02667 MINLOC = 42 MINVAL = 0.00222 I assume this is

Getting the minimum of the rows in a data frame

谁说胖子不能爱 提交于 2019-11-28 12:11:05
I am working with a dataframe that has 65 variables in it. The first variable catalogs a person, and the next 64 variables indicate the geographic distance that person is from each of 64 locations. Using R, I would like to create a new variable that catalogs the shortest distance for each person to one of those 64 locations. For example: if person X is 35, 50, 79, 100, 450...miles away from the locations, I would like the new variable to automatically assign them a 35, because this is the shortest distance. Any help with this would be much appreciated. Thanks. df <- data.frame(let=letters[1:25

How to apply min and max on textarea?

好久不见. 提交于 2019-11-28 11:58:28
Is there a way in HTML5 forms to add a min and max character to a textarea? It seems I can apply it to a regular input using pattern. <input pattern=".{3,}" title="3 characters minimum"> Am I looking at a jquery solution? HTML5 solution, min 5, max 20 characters just set the attribute maxlength="20" and minlength="5" to the textarea tag http://jsfiddle.net/xhqsB/603/ <form> <textarea maxlength="20" minlength="5"></textarea> <input type="submit" value="Check"></input> </form> For max: see below code Use maxlength for maximum character. maxlength="nuber_of_characters" Source: http://www.w3.org