C + + implements MATLAB One-dimensional array function Findpeaks () function to find one-dimensional discrete extremum (crest trough)

Source: Internet
Author: User
Tags diff


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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.