Matlab 2c uses c + + to realize matlab function series tutorial-unique function

Keywords: MATLAB

Full Stack Engineer Development Manual (Author: Luan Peng)

Download of MATLAB 2C dynamic link library
matlab library functions
Matlab 2C basic course
Matlab 2C development full-text course

matlab2c calls method:

1. Download matlab 2C dynamic link library
2. Put matlab 2c.dll, matlab 2c.lib and MATLAB 2c.h in the project header file directory.
3. Introduce the following code into the cpp file

#include "Matlab2c.h"
#pragma comment(lib,"Matlab2c.lib")  
using namespace Matlab2c;

Introduction of unique function in matlab

1. unique function:

Remove duplicate elements from a collection

2. Description of usage

[ C,IA,IC ] = unique(A)
IA As matrix C The element in the matrix A In the middle position,
IC As matrix A The element in the matrix C Position in

c + + source code implementation of unique

Single-valued elements for collections
Input: Set a
Output: Set pointer. The first set represents the sort of set after deleting duplicate elements from set a. The second set is the position of new set elements in the original set a. The third set is the position of the original set elements in the new set.
Tip: Position subscripts start at 0

Matrix* Matlab2c::unique(Matrix& a)
{
    bool exist=false;
    vector<double> c;

    Matrix *p; 
    p = new Matrix[3];
    for (int j=0;j<a.row*a.column;j++)
    {
        exist=false;
        for (int n=0;n<c.size();n++)
            if (a.data[j]==c[n])
            {
                exist=true;
                break;
            }
            if (!exist)
                c.push_back( a.data[j]);  
    }

    Matrix ia(1,c.size());
    Matrix ib(1,a.row*a.column); 
    Matrix tmp(c,1,c.size());
    Matlab2c::Q_sort(tmp);  //Quick sort
    for (int i=0;i<a.row*a.column;i++)
    {
        for (int j=0;j<tmp.row*tmp.column;j++)
        {
            if(a(i)==tmp(j))
            {
                ia(j)=i;
                ib(i)=j;
            }
        }
    }

    *p = tmp;
    *(p+1)=ia;
    *(p+2)=ib;
    return p;
}

Usage Test of unique Function

#include "Matlab2c.h"
#pragma comment(lib,"Matlab2c.lib")  
using namespace Matlab2c;


int main()
{
    double a[]={1,1,2,4,4,3};
    Matrix aa=Matrix(1,6,a);
    Matrix* cc=Matlab2c::unique(aa);
    cout<<(*(cc+0)).toString()<<endl;
    cout<<(*(cc+1)).toString()<<endl;
    cout<<(*(cc+2)).toString()<<endl;
    system("pause");
    return 0;
}

Posted by JD^ on Mon, 11 Feb 2019 22:15:17 -0800