Click here to Skip to main content
15,935,259 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi
I want to implement Matlab ismember function in C++ or Delphi, but the result is more time consuming than Matlab result!

is there any idea?

thanks

What I have tried:

As I said, the Matlab output is more faster than my implementation in C++ and Delphi.

Here is my implementation in Delphi:
Delphi
TOutput2 = record
  Flag,Index : TBordar_Int32;
end;

function TMatlab.IsMember(const A, B: TBordar_Double): TOutput2;
var    
   i : Integer;
   j : Integer;
   k : Integer;
   Index,Flag : TBordar_Int32;
begin    
   SetLength(Index, Length(A));
   SetLength(Flag,Length(A));
   for i := 0 to Length(A) - 1 do
   begin
        for j := 0 to Length(B) - 1 do
        begin
            Flag[i] := 0;
            if A[i] = B[j] then
            begin
                Flag[i] := 1;
                Index[k] := j;
                inc(k);
                break;
            end;
            Flag[i] := 0;
        end;
    end;
    SetLength(Index,k);
    Result.Flag := Flag;
    Result.Index := Index;
end;
in this version, I changed the search algorithm to Binary Search:
Delphi
function TMatlab.IsMember2(A, B: TBordar_Double): TOutput2;
var    i : Integer;
    j : Integer;
    k : Integer;
    Index,Flag : TBordar_Int32;
    Len : Integer;    res : Integer;

    function bsearch(const v : Double) : Integer;
    var
        nlo,nhi,t : Integer;
    begin
        nlo := 0;
        nhi := Len-1;
        while nlo <> nhi do
        begin
            t := (nhi+nlo) div 2;
            if B[t] < v then
                nlo := t+1
            else
                nhi := t;
        end;
        if B[nhi]=v then
            result := nhi
        else
            result := -1;
    end;

begin
    SetLength(Index, Length(A));
    SetLength(Flag,Length(A));
    Len :=  Length(B);
    k := 0;
    for i := 0 to Length(A) - 1 do
    begin
        res := bsearch(A[i]);
        if res = -1 then
            Flag[i] := 0
        else
        begin
            Flag[i] := 1;
            Index[k] := j;
            inc(k);
        end;
    end;
    SetLength(Index,k);
    Result.Flag := Flag;
    Result.Index := Index;
end;
however the second version gets faster than first version, but Matlab version is faster yet. thank you
Posted
Updated 28-Sep-22 23:24pm
v3
Comments
Richard MacCutchan 20-Sep-22 4:19am    
Then your implementation may need improvement.
Moharram 28-Sep-22 0:57am    
I attached the implementation
CPallini 20-Sep-22 5:27am    
Show us your implementation, we could try to improve it.
Moharram 28-Sep-22 0:57am    
I attached the implementation
merano99 26-Sep-22 16:48pm    
How did you measure the times and what are the results?

1 solution

After multiple timings with 64 bit release code, I get times between 3us and 4us for the example.
I would be interested to know if the C++ solution takes longer and secondly how long Matlab takes.

Here is my test program call with the measurement used and the data:
C++
// input
vector<int> X{ 4, 6, 3, 2 };
vector<int> Y{ 1, 2, 4, 5, 3, 8 };

// output / result
vector<int> LX;
// LX = 1 0 1 1

auto t1 = high_resolution_clock::now();
LX = ismember(X, Y);
auto t2 = high_resolution_clock::now();

// Getting number of microseconds as a double
duration<double, std::micro> us_double = t2 - t1;
std::cout << us_double.count() << "us\n";

Of course there are many other ways to measure time. I have chosen the C++ way here. To avoid caching effects I restarted the program for each run.

If your code should take much longer you can show it. Then we can discuss about it.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900