Menu

how to combine two differnt GMatrix

Help
doityth777
2012-05-01
2012-09-14
  • doityth777

    doityth777 - 2012-05-01

    i have two GMatrix, like matrixA, matixB, and want to combine these into one
    GMatrix. after the operation, the resulting matrix has the relation name of
    matrixA, all the attr and data of both matrixA and matrixB. the function
    mergeHoriz(...) doesn`t meet the demand.

     
  • doityth777

    doityth777 - 2012-05-01

    matrixA, matixB have the same number of rows

     
  • Mike Gashler

    Mike Gashler - 2012-05-01

    Would this do what you want?

    1- use mergeHoriz

    2- copy the relation name

    Step 2 could be implemented like this:

    if(matrixA.relation()->type() == GRelation::ARFF)

    {

    GArffRelation pRelA = (GArffRelation)matrixA.relation().get();

    GArffRelation pRelMerged = (GArffRelation)pMatrixMerged->relation().get();

    pRelMerged->setName(pRelA->name());

    }

    else

    ThrowError("the relation of matrixA does not have a name");

    (I did not try to compile this, so it may contain bugs.)

     
  • doityth777

    doityth777 - 2012-05-01

    thanks for you r help!! it is solved!

    it is very easy to cast GArffRelation into GRelation, it is not clearly but by
    type() function to do inversely. i add some code in function mergHoriz() to do
    what i want above.

    GMatrix* GMatrix::mergeHoriz(GMatrix* pSetA, GMatrix* pSetB)
    {
        if(pSetA->rows() != pSetB->rows())
            ThrowError("Expected same number of rows");
        GArffRelation* pRel = new GArffRelation();
        sp_relation spRel;
        spRel = pRel;
        GRelation* pRelA = pSetA->relation().get();
        GRelation* pRelB = pSetB->relation().get();
        size_t nSetADims = pRelA->size();
        size_t nSetBDims = pRelB->size();
        pRel->addAttrs(pRelA);
        pRel->addAttrs(pRelB);
        if (pSetA->relation()->type() == GRelation::ARFF)
        {
            GArffRelation* p = (GArffRelation*)pSetA->relation().get();
            pRel->setName(p->name());
        }
        GMatrix* pNewSet = new GMatrix(spRel);
        Holder<GMatrix> hNewSet(pNewSet);
        pNewSet->reserve(pSetA->rows());
        double* pNewRow;
        for(size_t i = 0; i < pSetA->rows(); i++)
        {
            pNewRow = pNewSet->newRow();
            GVec::copy(pNewRow, pSetA->row(i), nSetADims);
            GVec::copy(&pNewRow[nSetADims], pSetB->row(i), nSetBDims);
        }
        return hNewSet.release();
    }
    
     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.