Return index of the smallest value in a vector?
You're looking for which.min()
:
a <- c(1,2,0,3,7,0,0,0)
which.min(a)
# [1] 3
which(a == min(a))
# [1] 3 6 7 8
(As you can see from the above, when several elements are tied for the minimum, which.min()
only returns the index of the first one. You can use the second construct if you instead want the indices of all elements that match the minimum value.)
How to get the index of the smallest element in a vector?
You can also do this:
std::vector<int>::iterator it = std::min_element(std::begin(vec), std::end(vec));
std::cout << "index of smallest element: " << std::distance(std::begin(vec), it);
or even simpler:
auto it = std::min_element(std::begin(vec), std::end(vec));
std::cout << "index of smallest element: " << std::distance(std::begin(vec), it);
or:
std::cout << "index of smallest element: " <<
std::distance(std::begin(v), std::min_element(std::begin(v), std::end(v)))
How to get the index of the minimum value of an array
Base are
which(centopulos==min(centopulos))
But maybe there is a more fancy way
C++ retrieving an index value in a vector while using min_element or max_element
std::min_element()
and std::max_element()
work with iterators, not indexes.
For an indexable container like std::vector
, you can convert an iterator to an index using std::distance()
, eg:
std::string getTopEmployee( const std::vector<std::string>& names, const std::vector<unsigned>& ratings ) {
// Find Largest Value in ratings
auto largest = std::max_element( ratings.begin(), ratings.end() );
if (largest == ratings.end()) return "";
return names[std::distance(ratings.begin(), largest)];
}
std::string getWorstEmployee( const std::vector<std::string>& names, const std::vector<unsigned>& ratings ) {
// Find Smallest Value in ratings
auto smallest = std::min_element( ratings.begin(), ratings.end() );
if (smallest == ratings.end()) return "";
return names[std::distance(ratings.begin(), smallest)];
}
How to find the vector element that indexes the lowest value in an array
This is taken straight from http://en.cppreference.com/w/cpp/algorithm/min_element
If i understand correctly you want to get the index of the minimal element in an array
This should work both with vectors and arrays alike
std::vector<int> v{3, 1, 4, 1, 5, 9};
// We need to get the min value
std::vector<int>::iterator result = std::min_element(std::begin(v), std::end(v));
// Then we get the index of the value in the array
std::cout << "min element at: " << std::distance(std::begin(v), result);
What you can do is first get all the values of Cost array in the position of id values and store them in an temporary std::vector.
According to your code, you can use a iterator over the values contained in ids, inside the loop you use std::vector push_back() function to add the elements in position of Cost[id].
Then apply the above mentioned std::min_element and std::distance to get the index. Note that this will return the index of the id vector, getting the value from there is just a matter of ids[index]
Julia Questions: Find n-smallest value's index in vector
Use partialsortperm
:
julia> partialsortperm([1, 5, 6, 7, 8, 9, 10, 2, 3, 4], 1:5)
5-element view(::Vector{Int64}, 1:5) with eltype Int64:
1
8
9
10
2
julia> partialsortperm([1, 8, 6, 7, 8, 9, 10, 2, 3, 4], 1:5)
5-element view(::Vector{Int64}, 1:5) with eltype Int64:
1
8
9
10
3
How can I find the index of the smallest member of this vector in Clojure?
user=> (first (apply min-key second (map-indexed vector [1 2 4 0 5])))
3
Related Topics
In Ggplot2, Coord_Flip and Free Scales Don't Work Together
How to Add Annotations Below the X Axis in Ggplot2
Calculate Euclidean Distance Matrix Using a Big.Matrix Object
Propagating Data Within a Vector
Reshaping Data Frame with Duplicates
Accurately Converting from Character->Posixct->Character with Sub Millisecond Datetimes
Image Not Showing in Shiny App R
Rstudio Is Duplicating Commands in the Command Line
Applying a Function to Two Lists
Remove All Duplicates Except Last Instance
Heatmap-Like Plot, But for Categorical Variables
Assign a Value, If a Number Is in Between Two Numbers
Identify Duplicates and Mark First Occurrence and All Others
Warning Message: Line Appears to Contain Embedded Nulls
Why I Get This Error Writing Data to a File
How to Use R to Download a Zipped File from a Ssl Page That Requires Cookies
How to Scrape/Automatically Download PDF Files from a Document Search Web Interface in R