XLL+ Class Library

mtx_ptrs<T> Example

// Function:    MatrixAxisMean
// Purpose:     Returns the mean of the values in one axis of a matrix

//{{XLP_SRC(MatrixAxisMean)
    // NOTE - the FunctionWizard will add and remove mapping code here.
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN2(MatrixAxisMean, "RPI", "MatrixAxisMean", 
    "m,axis", "Math & Trig", "Returns the mean of the values in"
    " one axis of a matrix", "Square input matrix\000Axis - 0 or"
    " 1\000", "B0()0()m Square input matrix\0\0", 1)

extern "C" __declspec( dllexport )
LPXLOPER MatrixAxisMean(const COper* m, short axis)
{
    CXlOper xloResult;
    BOOL bOk = TRUE;
    MTX_PTRS<double> matm;
    bOk = bOk && m->ReadMatrix(matm, "m", xloResult, 0, 0, XLA_ARRAY_FLAGS_STD, 0, 0);
    if (!bOk)
        return xloResult.Ret();
//}}XLP_SRC

    // Check that the matrix is square & not empty
    if (matm.size(0) != matm.size(1))
        return CXlOper::RetString("#Error: Expected a square matrix for m");
    if (matm.size(0) == 0)
        return CXlOper::RetString("#Error: Expected a non-empty square matrix for m");

    // Traverse the matrix
    int i, n = matm.size(0);
    double dSum = 0.0;
    for (i = 0; i < n; i++)
        dSum += (axis == 0) ? matm[i][i] : matm[i][n - (1 + i)];
    dSum /= (double)n;

    // Return the result
    xloResult = dSum;
    return xloResult.Ret();
}

Uses

mtx_ptrs<T> | mtx_ptrs<T>::size | mtx_ptrs<T>::operator [] const