One, the code
#include <iostream> #include <vector>using namespace std;////c++ realization of one-dimensional discrete data extremum (crest, trough)////parameter: array, array size void Findpeaks (int *num,int count) {vector<int> sign;for (int i = 1;i<count;i++) {/* Adjacent values do poor: * Less than 0, assignment-1 * greater than 0, 1 * equals 0, 0 */ int diff = num[i]-num[i-1];if (diff>0) {sign.push_back (1);} else if (diff<0) {sign.push_back (-1);} Else{sign.push_back (0);}} Then make the difference between the sign adjacent bit//save the maximum and minimum position vector<int> indmax;vector<int> indmin;for (int j = 1;j<sign.size (); j + +) { int diff = sign[j]-sign[j-1];if (diff<0) {indmax.push_back (j);} else if (diff>0) {indmin.push_back (j);}} cout<< "Maximum value:" <<endl;for (int m = 0;m<indmax.size (); m++) {cout<<num[indmax[m]]<< " ";} cout<<endl;cout<< "Minimum:" <<endl;for (int n = 0;n<indmin.size (); n++) {Cout<<num[indmin[n]] << " ";}} int main () {int a[] = {1,2,10,2,4,1,8,10,23,0};findpeaks (a,10); return 0;}
Second, the operation result:
Third, using MATLAB to verify the results
C + + implements MATLAB One-dimensional array function Findpeaks () function to find one-dimensional discrete extremum (crest trough)