assign3/mat.cpp
// pick from array those rows where list has an element equal to match.
// can be used to do a complex selection from an array based on 1 and 0 in another array
// or even a variety of values stored in column vector list
// WARNING: allocates new matrix for answer
pick from array those rows where list has an element equal to match.
can be used to do a complex selection from an array based on 1 and 0 in another array
or even a variety of values stored in column vector list
WARNING: allocates new matrix for answer
pick from array those rows where list has an element equal to match . can be used to do a complex selection from an array based on 1 and 0 in another array or even a variety of values stored in column vector list WARNING : allocates new matrix for answer
Matrix Matrix::pickRows(int match, const Matrix &list, int &num)
{
    assertDefined("lhs of pickRows");
    list.assertDefined("rhs of pickRows");
    list.assertColVector("rhs of pickRows");
    assertRowsEqual(list, "pickRows");
    num = 0;
    for (int r=0; r<maxr; r++) if (list.m[r][0]==match) num++;
    if (num==0) {
        Matrix out("Undefined");
        return out;
    }
    Matrix out(num, maxc);
    { int rr=0;
        for (int r=0; r<maxr; r++) {
            if (list.m[r][0]==match) {
                for (int c=0; c<maxc; c++) {
                    out.m[rr][c] = m[r][c];
                }       
                rr++;
                if (rr>=num) break;
            }
        }
    }
    out.defined = true;
    return out;
}
Matrix Matrix :: pickRows ( int match , const Matrix & list , int & num ) { assertDefined ( " " ) ; list . assertDefined ( " " ) ; list . assertColVector ( " " ) ; assertRowsEqual ( list , " " ) ; num = 0 ; for ( int r = 0 ; r < maxr ; r ++ ) if ( list . m [ r ] [ 0 ] == match ) num ++ ; if ( num == 0 ) { Matrix out ( " " ) ; return out ; } Matrix out ( num , maxc ) ; { int rr = 0 ; for ( int r = 0 ; r < maxr ; r ++ ) { if ( list . m [ r ] [ 0 ] == match ) { for ( int c = 0 ; c < maxc ; c ++ ) { out . m [ rr ] [ c ] = m [ r ] [ c ] ; } rr ++ ; if ( rr >= num ) break ; } } } out . defined = true ; return out ; }
pick from array those rows where list has an element equal to match.
pick from array those rows where list has an element equal to match .