Matlab 2c uses c + + to realize matlab function series tutorial-find 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;

Brief introduction of find function in matlab

1. find function:

find function is used to return the location of the required elements (location determination: in the matrix, the first column begins, top-down, followed by 1, 2, 3... And then from the second column to the third column.

2. Description of usage

find (A) Returns the location of non-zero elements in matrix A
find (A > 5) returns the location of elements greater than 5 in matrix A
[i,j,v]=find(A) returns row i, column j, and element value v (output in order of location) of non-zero elements in A.

c + + source code implementation of find

Find element location
Input: Input is a vector or matrix, find the number m
Output: the position of m in a vector or matrix
Tip: When the input is a vector, the output is a column vector, and the position subscript begins at 0.
When the input is a matrix, the output is a matrix. The first column is the row where the element is located, the second column is the column where the element is located, and the third column is the element value.

Matrix Matlab2c::find(Matrix& a,double m)  
{
    //If it's a vector
    if (a.isVector())
    {
        vector<int> location;
        for (int i=0;i<a.row*a.column;i++)
            if(a(i)!=m)
                location.push_back(i);
        Matrix p(location.size,1);
        for (int i=0;i<p.row;i++)
            p(i)=location[i];
        return p;
    }
    //If it is a matrix
    else
    {
        vector<int> x,y;
        vector<double> value;
        for (int i=0;i<a.row;i++)
            for (int j=0;j<a.column;j++)
                if(a(i)!=m)
                {
                    x.push_back(i);
                    y.push_back(j);
                    value.push_back(a(i,j));
                }
        Matrix p(x.size,3);
        for (int i=0;i<p.row;i++)
        { 
            p(i,0)=x[i];
            p(i,1)=y[i];
            p(i,2)=value[i];
        }
        return p;
    }
}
Matrix Matlab2c::find(Matrix& a) 
{
    return find(a,0);
}

Use Test of find Function

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


int main()
{
    double a[]={1,2,3,4,1,2,3,4,};
    Matrix aa=Matrix(2,4,a);
    Matrix bb=Matrix(1,8,a);
    Matrix cc=Matlab2c::find(aa,1);
    cout<<cc.toString()<<endl;
    Matrix dd=Matlab2c::find(bb,1);
    cout<<dd.toString()<<endl;


    system("pause");
    return 0;
}

Posted by peanutbutter on Sun, 10 Feb 2019 07:12:19 -0800