-1

I want to get each element in this : vector x so I can compare each element to get max and min value in each Mat in this Vector i'm trying to do this but get exception in :if (max <= refG[i].at(j,k))

int max_Val(vector<Mat> refG)
            {
        refG.reserve(m);
                int max = 0;
                for (int i = 0; i < m; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        for (int k = 0; k < s; k++)
                        {
                            if (max <= refG[i].at<int>(j,k))
                            {
                                max = refG[i].at<int>(j,k);
                            }
                        }
                    }
                }
                return max;
            }

2 Answers2

0

Initialize max and min to the vector's first element and then with a loop you can access all the other elements and compare them with max and min. Something like

max = min = x[0];
for (int i = 1; i < x.size(); ++i) {
   if (x[i] < min) min = x[i];
   else if (x[i] > max) max = x[i];
}
Riemann
  • 1
  • 1
0

Alternative way to find MAX and MIN in vector: *max_element (first_index, last_index); *min_element (first_index, last_index);

EX: vector a = { 1, 45, 54, 71, 76, 12 };

// Print the vector 
cout << "Vector: "; 
for (int i = 0; i < a.size(); i++) 
    cout << a[i] << " "; 
cout << endl; 

// Find the max element 
cout << "\nMax Element = "
     << *max_element(a.begin(), a.end());  // Max = 76