C++ 6.0 intersection of two sets

C++ 6.0 intersection of two sets

Post by Rews » Fri, 07 Nov 2003 13:19:09



I have been working * writing a C++ program using the 6.0
version on write a function that finds the intersection of two sets.
Part of the problem is to get the the function to find all the prime
numbers less than a certain value using the above set intersection
function. Can you send me a program that can handle the above problem.
Please try. Anxiously awaiting your reply!
 
 
 

C++ 6.0 intersection of two sets

Post by DO » Fri, 07 Nov 2003 17:59:05



> I have been working * writing a C++ program using the 6.0
> version on write a function that finds the intersection of two sets.
> Part of the problem is to get the the function to find all the prime
> numbers less than a certain value using the above set intersection
> function. Can you send me a program that can handle the above problem.
> Please try. Anxiously awaiting your reply!

You are entirely off-topic in this group.

Since you are using C++ you can use STL functions to do set a
intersection. Look at the function set_intersection() contained in
<algorithm>.

If this does not fit your needs you should direct your question to a C++
group.

HTH,
   Herbert

 
 
 

C++ 6.0 intersection of two sets

Post by AJ \ » Fri, 07 Nov 2003 21:18:16




> > I have been working * writing a C++ program using the 6.0
> > version on write a function that finds the intersection of two sets.
> > Part of the problem is to get the the function to find all the prime
> > numbers less than a certain value using the above set intersection
> > function. Can you send me a program that can handle the above problem.
> > Please try. Anxiously awaiting your reply!

> You are entirely off-topic in this group.

> Since you are using C++ you can use STL functions to do set a
> intersection. Look at the function set_intersection() contained in
> <algorithm>.

> If this does not fit your needs you should direct your question to a C++
> group.

> HTH,
>    Herbert

----------- begin file setintersect.m ----------
%SETINTERSECT - Find intersection of any number of (type double) sets
%
%  SETINTERSECT(A,...) returns the elements that all arguments A,... have
%  in common. This is different than INTERSECT which returns only unique
%  elements.
%
%  Examples:
%  setintersect(factor(432),factor(810),factor(504)) returns [2 3 3]
%  setintersect(factor(132),factor(245)) returns []

----------- end file setintersect.m ----------

----------- begin file setintersect.cpp ----------
//--------------------------------------------------------------
// file: setintersect.cpp - Matlab Mex function application
//--------------------------------------------------------------
#include <iostream.h>
#include <math.h>

extern "C" {
#include "mex.h"

Quote:}

//--------------------------------------------------------------
// function: setintersect - Entry point from Matlab environment (via
//   mexFucntion(), below)
// INPUTS:
//   nlhs - number of left hand side arguments (outputs)
//   plhs[] - pointer to table where created matrix pointers are
//            to be placed
//   nrhs - number of right hand side arguments (inputs)
//   prhs[] - pointer to table of input matrices
//--------------------------------------------------------------
void setintersect( int nlhs, mxArray *plhs[], int nrhs, const mxArray
*prhs[] )
{
  int m=-1; // Upper bound on intersection size
  int r; // iteration counter for right hand arguments
  int rmin; // RH argument with the fewest number of elements
  int e; // iteration counter for elements being processed
  double *pData; // Pointer to data in RH argument
  int i; // iteration counter for elements in intersection

  // Find the set with the smallest number of elememts
  for (r=0; r<nrhs; r++)
  {
    if (!mxIsDouble(prhs[r]))
      mexErrMsgTxt("All input arguments must be double");
    if (m < 0 || m > mxGetNumberOfElements(prhs[r]))
    {
      m = mxGetNumberOfElements(prhs[r]);
      rmin = r;
    }
  }

  // If empty arrays (or no arrays), return empty set
  if (m <= 0)
  {
    plhs[0] = mxCreateDoubleMatrix(0,0,mxREAL);
    return;
  }

  double *intersection = new double [m];
  bool *matchflag = new bool [m]; // Used with each iteration loop
  bool *keepflag = new bool [m]; // Any element not in intersection set to
false.

  // Initialize keepflag[] array to all 1's
  for (i=0; i<m; i++)
    keepflag[i] = true;

  // Start the intersection set as the elements of the smallest set
  pData = (double *)mxGetData(prhs[rmin]);
  for (e=0; e<m; e++)
  {
    intersection[e] = pData[e];
  }

  // Process each RH array in turn
  for (r=0; r<nrhs; r++)
  {
    // Set matchflag[] array to all false; set back to true as
    //   elements from next set are determined to be in intersection.
    for (i=0; i<m; i++)
      matchflag[i] = false;

    // Loop though all elements in the set
    int E=mxGetNumberOfElements(prhs[r]); // Make loop a little faster
    pData = (double *)mxGetData(prhs[r]);
    for (e=0; e<E; e++)
    {
      double val = pData[e];
      // See if the element belongs in the intersection.
      for (i=0; i<m; i++)
        if (keepflag[i] && !matchflag[i] && intersection[i] == val)
        {
          matchflag[i] = true;
          break;
        }
    }

    // Don't keep any elements not mathced to the intersection
    for (i=0; i<m; i++)
      keepflag[i] = matchflag[i];
  }

  // Count the number of elements in intersection
  int n = 0;
  for (i=0; i<m; i++)
    if (keepflag[i])
      n++;

  // Create an output array for the intersection data
  plhs[0] = mxCreateDoubleMatrix(1,n,mxREAL);
  pData = (double *)mxGetData(plhs[0]);

  // move the elements in intersection to output array
  for (i=0; i<m; i++)
    if (keepflag[i])
      *(pData++) = intersection[i];

  // clean up
  delete keepflag;
  delete matchflag;
  delete intersection;

Quote:} // end setintersect()

extern "C" {
  //--------------------------------------------------------------
  // mexFunction - Entry point from Matlab. From this C function,
  //   simply call the C++ application function, above.
  //--------------------------------------------------------------
  void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray
*prhs[] )
  {
    setintersect(nlhs, plhs, nrhs, prhs);
  }
Quote:}

----------- end file setintersect.cpp ----------

-Aj

 
 
 

1. Finding the intersection of two sets of data

Once again I ask a question of the knowledgable peopl in this group.

I have two sets of data say [a] and [b], that can each be plotted on
the cartesian plane. However the x coordinates used for each data set
are different. Is there a quick way to find the point (interpolated
between the data points) where the data intersects.

The only method that I have thought of to do this is to compare every
interval of [a] to every interval of [b] to see if the line segments
joining the points intersect. This equires length(a)*length(b)
iterations which is very time consuming.

ie if I had a simple parabola, y = x2, and a simple line y = x.
The parabola's data is given for the range x=0.1:0.1:1,
  whilst the line's is given for the range x = 0.05:0.1:1.05

As you can see none of the data points line up, but it still has to
identify approximately x = 1, as the point where they intersect.

Oh, while I was typing I thought about using a polynomial to
approximate each line. But this would also require very large numbers
of calcs to cover the entire data. How about doing this for the one
curve that remains constant, and then only working with the one curve
that does change each iteration.

Any ideas. Please help.

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

2. Hub and switches

3. intersection of two curves (in two space)

4. WTB : SUN SM100 or 121 processors

5. How to set up options files for Matlab 5.2 and Microsoft Visual C++ 6.0?

6. Stupid question re : autoexec.ncf

7. Help using Matlab C++ Math library in Visual C++ 6.0

8. WYSE50 support

9. How to Use Matlab C++ Libraries for Visual C++ 6.0

10. RTW C++ Code for Visual C++ 6.0 -help!

11. Use of translated C/C++ files with MS Visual C++ 6.0 IDE

12. Simulink intersection of two signals

13. Intersection of two spheres