Return conditional from mrdivide

1 visualizzazione (ultimi 30 giorni)
Samuel
Samuel il 29 Mag 2017
Commentato: Samuel il 1 Set 2017
I'm trying to optimize the run time of my subroutine. I'm using mex files. The subroutine calls mrdivide. Depending on the input parameters, sometimes the solution blows up, and when that happens, Matlab spits out:
Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = 1.27943e-18.
When it blows up, I just want the function to return. So what i do is check the conditional before I call mrdivide. My code looks like this:
while (!broken && !done)
{
mexCallMATLAB(1, cond_lhs, 1, cond_rhs, "cond");
cond = mxGetScalar(cond_lhs[0]);
mxDestroyArray(cond_lhs[0]);
if (cond > 1e15) // MATLAB STARTS WARNINGS AT 1e16 and 1e17 ish
{
broken = 3; // breaks out of loop
}
else
{
mexCallMATLAB(1, mrdivider_lhs, 2, mrdivider_rhs, "mrdivide");
OTHER CODE HERE. Does other things, including updating done or !done
}
}
and, my solution works! The problem is that checking the conditional before calling mrdivide doubles my run time. Calling cond(matrix) requires Matlab to do the SVD, which is the most time consuming part of my loop. If the conditional is small enough, matlab has to do the same exact thing over again (and more) in mrdivide.
I want to get the conditional from mrdivide. As in mrdivide returns both the matrix and the conditional so I can check conditional and adjust if necessary. Is there a way for me to do that?

Risposta accettata

Christine Tobler
Christine Tobler il 30 Mag 2017
You could use the linsolve function, with a second output argument:
[x, rcond] = linsolve(A, b);
This solves the linear system A*x = b, suppresses the warning given by mldivide/mrdivide, and returns an estimate of the condition number of A in rcond. You could check if rcond < eps, and break out of the loop in that case.
Since this is similar to mldivide, not to mrdivide, unfortunately you would have to transpose the inputs and outputs to linsolve.

Più risposte (1)

John D'Errico
John D'Errico il 29 Mag 2017
Modificato: John D'Errico il 29 Mag 2017
MRDIVIDE does not return an exit flag of any sort.
You can always just use pinv instead of slash. It will be robust to singular systems. Of course, you may not be happy with the result then.
Or, you can do the solve yourself, using a column pivoted QR factorization. If the diagonals of R have sufficiently small elements (relative to the max) then don't finish doing the solve. No warning will ever be generated, and you can do as you wish.
You COULD use the lastwarn function to test to see if slash kicked out a warning, and if so, then make a decision. The one thing I would NEVER do is to turn off that warning. Warnings were put there for a good reason.

Categorie

Scopri di più su Linear Algebra in Help Center e File Exchange

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by