void 
PeakViewMerge::myProcess(realvec& in, realvec& out)
{
	peakView	*In[kNumMatrices],
				Out (out);
	mrs_natural i, rowIdx = 0,
				numPeaks[kNumMatrices],
				outputIdx	= 0;
	const mrs_bool discNegGroups	= ctrl_noNegativeGroups_->to<mrs_bool>();

	out.setval(0.);
	
	for (i = 0; i < kNumMatrices; i++)
	{
		mrs_natural	numRows		= (i==kMat1)? ctrl_frameMaxNumPeaks1_->to<mrs_natural>() :  ctrl_frameMaxNumPeaks2_->to<mrs_natural>();
		numRows					*= peakView::nbPkParameters;
		if (numRows == 0) // if the controls have not been set assume both matrixes to be of equal size	
			numRows	= in.getRows ()/kNumMatrices;
		peakViewIn_[i].stretch (numRows, in.getCols ());
		in.getSubMatrix (rowIdx, 0, peakViewIn_[i]);
		rowIdx		+= numRows;
		In[i]		= new peakView(peakViewIn_[i]);
		numPeaks[i]	= In[i]->getTotalNumPeaks ();
	}

	if (ctrl_mode_->to<mrs_string>() == "OR")
	{
		// write all entries of the second peakView to output
		for (i = 0; i < numPeaks[1]; i++)
		{
			if (discNegGroups && (*In[1])(i,peakView::pkGroup) < 0)
				continue;
			WriteOutput (Out, In[1], i, outputIdx);
			outputIdx++;
		}

		// write all entries of the first peakView to output except duplicates
		for (i = 0; i < numPeaks[0]; i++)
		{
			mrs_natural Idx;
			if (discNegGroups && (*In[0])(i,peakView::pkGroup) < 0)
				continue;
			for (mrs_natural k = 1; k < kNumMatrices; k++)
				Idx	= FindDuplicate (In[k], (*In[0])(i, peakView::pkFrequency), numPeaks[k]);

			if (Idx < 0)
			{
				WriteOutput (Out, In[0], i, outputIdx);
				outputIdx++;
			}
		}
	}
	else if (ctrl_mode_->to<mrs_string>() == "AND")
	{
		// find duplicates and write only them to output
		for (i = 0; i < numPeaks[0]; i++)
		{
			mrs_natural Idx;
			if (discNegGroups && (*In[0])(i,peakView::pkGroup) < 0)
				continue;
			for (mrs_natural k = 1; k < kNumMatrices; k++)
				Idx	= FindDuplicate (In[k], (*In[0])(i, peakView::pkFrequency), numPeaks[k]);

			if (Idx >= 0)
			{
				if (discNegGroups && (*In[1])(Idx,peakView::pkGroup) < 0)
					continue;
				WriteOutput (Out, In[0], i, outputIdx);
				outputIdx++;
			}
		}
	}
	else if (ctrl_mode_->to<mrs_string>() == "ANDOR")
	{
		// keep the input[0] peaks that are not in input[1]
		for (i = 0; i < numPeaks[0]; i++)
		{
			mrs_natural Idx;
			if (discNegGroups && (*In[0])(i,peakView::pkGroup) < 0)
				continue;
			for (mrs_natural k = 1; k < kNumMatrices; k++)
				Idx	= FindDuplicate (In[k], (*In[0])(i, peakView::pkFrequency), numPeaks[k]);

			if (Idx < 0)
			{
				WriteOutput (Out, In[0], i, outputIdx);
				outputIdx++;
			}
		}
	}
	else if (ctrl_mode_->to<mrs_string>() == "XOR")
	{
		// find duplicates and write only residual to output
		for (i = 0; i < numPeaks[0]; i++)
		{
			if (discNegGroups && (*In[0])(i,peakView::pkGroup) < 0)
				continue;
			mrs_natural Idx	= FindDuplicate (In[1], (*In[0])(i, peakView::pkFrequency), numPeaks[1]);

			if (Idx < 0)
			{
				WriteOutput (Out, In[0], i, outputIdx);
				outputIdx++;
			}
		}
		// find duplicates and write only residual to output
		for (i = 0; i < numPeaks[1]; i++)
		{
			if (discNegGroups && (*In[1])(i,peakView::pkGroup) < 0)
				continue;
			mrs_natural Idx= FindDuplicate (In[0], (*In[1])(i, peakView::pkFrequency), numPeaks[0]);

			if (Idx < 0)
			{
				WriteOutput (Out, In[1], i, outputIdx);
				outputIdx++;
			}
		}
	}
	else 
	{
		MRSERR("PeakViewMerfe::myProcess() : illegal mode string: " << ctrl_mode_->to<mrs_string>());
	}

	for (i = 0; i < kNumMatrices; i++)
	{
		delete In[i];
	}

	ctrl_totalNumPeaks_->setValue(outputIdx);
}