Commit af25d30a authored by Davis King's avatar Davis King

Added functions to find the index of the min and max element of a vector.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402877
parent 050b2409
......@@ -33,7 +33,7 @@ namespace dlib
{
DLIB_ASSERT(m.size() > 0,
"\ttype find_min_and_max(const matrix_exp& m, min_val, max_val)"
<< "\n\tYou can't ask for the max() of an empty matrix"
<< "\n\tYou can't ask for the min and max of an empty matrix"
<< "\n\tm.size(): " << m.size()
);
typedef typename matrix_exp<EXP>::type type;
......@@ -53,6 +53,70 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
template <
typename EXP
>
long index_of_max (
const matrix_exp<EXP>& m
)
{
DLIB_ASSERT(m.size() > 0 && (m.nr() == 1 || m.nc() == 1),
"\tlong index_of_max(const matrix_exp& m)"
<< "\n\tm must be a row or column matrix"
<< "\n\tm.size(): " << m.size()
<< "\n\tm.nr(): " << m.nr()
<< "\n\tm.nc(): " << m.nc()
);
typedef typename matrix_exp<EXP>::type type;
type val = m(0);
long best_idx = 0;
for (long i = 1; i < m.size(); ++i)
{
type temp = m(i);
if (temp > val)
{
val = temp;
best_idx = i;
}
}
return best_idx;
}
// ----------------------------------------------------------------------------------------
template <
typename EXP
>
long index_of_min (
const matrix_exp<EXP>& m
)
{
DLIB_ASSERT(m.size() > 0 && (m.nr() == 1 || m.nc() == 1),
"\tlong index_of_min(const matrix_exp& m)"
<< "\n\tm must be a row or column matrix"
<< "\n\tm.size(): " << m.size()
<< "\n\tm.nr(): " << m.nr()
<< "\n\tm.nc(): " << m.nc()
);
typedef typename matrix_exp<EXP>::type type;
type val = m(0);
long best_idx = 0;
for (long i = 1; i < m.size(); ++i)
{
type temp = m(i);
if (temp < val)
{
val = temp;
best_idx = i;
}
}
return best_idx;
}
// ----------------------------------------------------------------------------------------
template <
......
......@@ -638,6 +638,40 @@ namespace dlib
over the elements of the matrix m.
!*/
// ----------------------------------------------------------------------------------------
template <
typename EXP
>
long index_of_max (
const matrix_exp<EXP>& m
);
/*!
requires
- m.nr() == 1 || m.nc() == 1 (i.e. m must be a row or column vector)
- m.size() > 0
ensures
- returns the index of the largest element in m.
(i.e. m(index_of_max(m)) == max(m))
!*/
// ----------------------------------------------------------------------------------------
template <
typename EXP
>
long index_of_min (
const matrix_exp<EXP>& m
);
/*!
requires
- m.nr() == 1 || m.nc() == 1 (i.e. m must be a row or column vector)
- m.size() > 0
ensures
- returns the index of the smallest element in m.
(i.e. m(index_of_min(m)) == min(m))
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp::type sum (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment