Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fwd-slash division

Subject: fwd-slash division

From: Hydroman S

Date: 26 Nov, 2008 20:50:18

Message: 1 of 14

Consider the solution for: xA=b, where x=b/A.

A=magic(3);
b=[1 2 3];
x= b/A;

ans =

-0.03333333333333 0.46666666666667 -0.03333333333333

I want to perform this fwd-slash division for only one element of x, say x(:,2)

So I tried:

b(:,2)/A(2,2)
but it gave me
0 0 0.22222222222222

Can someone tell what’s wrong in what I’m doing?

Subject: fwd-slash division

From: Hydroman S

Date: 26 Nov, 2008 21:00:20

Message: 2 of 14

"Hydroman S" <amirgsalem@gmail.com> wrote in message <ggkcqa$m7j$1@fred.mathworks.com>...
> Consider the solution for: xA=b, where x=b/A.
>
> A=magic(3);
> b=[1 2 3];
> x= b/A;
>
> ans =
>
> -0.03333333333333 0.46666666666667 -0.03333333333333
>
> I want to perform this fwd-slash division for only one element of x, say x(:,2)
>
> So I tried:
>
> b(:,2)/A(2,2)
> but it gave me
> 0 0 0.22222222222222
>
> Can someone tell what’s wrong in what I’m doing?

sorry there was a typo in:
b(:,2)/A(2,2)

it should have been
b(:,2)/A(:,2)
to give
0 0 0.22222222222222

but the question is still holds, how to perform this division so that the output is the second ement of x only?

Subject: fwd-slash division

From: Matt Fig

Date: 26 Nov, 2008 21:04:02

Message: 3 of 14

> b(:,2)/A(2,2)
> but it gave me
> 0 0 0.22222222222222
>
> Can someone tell what’s wrong in what I’m doing?


>> b(:,2)/A(2,2) % On my machine:

ans =

   0.400000000000000

Subject: fwd-slash division

From: Bruno Luong

Date: 26 Nov, 2008 21:59:02

Message: 4 of 14

"Hydroman S" <amirgsalem@gmail.com> wrote in message <ggkdd4$1d0$1@fred.mathworks.com>...

>
> but the question is still holds, how to perform this division so that the output is the second ement of x only?
>

b*subsref(inv(A),substruct('()',{':',2}))

You might revise linear algebra if you ever wonder why there is no shorter way.

Bruno

Subject: fwd-slash division

From: Hydroman S

Date: 27 Nov, 2008 00:23:02

Message: 5 of 14

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ggkgr6$g98$1@fred.mathworks.com>...
> "Hydroman S" <amirgsalem@gmail.com> wrote in message <ggkdd4$1d0$1@fred.mathworks.com>...
>
> >
> > but the question is still holds, how to perform this division so that the output is the second ement of x only?
> >
>
> b*subsref(inv(A),substruct('()',{':',2}))
>
> You might revise linear algebra if you ever wonder why there is no shorter way.
>
> Bruno

Thank you Bruno.........good answers call for more questions:
1- people often warn against using “inv” function in Matlab, is it ok to use it in this case
2- I will definitely go back to linear algebra, but what area can I find a good explanation that addresses this section?

Subject: fwd-slash division

From: Hydroman S

Date: 27 Nov, 2008 02:55:03

Message: 6 of 14

"Hydroman S" <amirgsalem@gmail.com> wrote in message <ggkp95$d4r$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ggkgr6$g98$1@fred.mathworks.com>...
> > "Hydroman S" <amirgsalem@gmail.com> wrote in message <ggkdd4$1d0$1@fred.mathworks.com>...
> >
> > >
> > > but the question is still holds, how to perform this division so that the output is the second ement of x only?
> > >
> >
> > b*subsref(inv(A),substruct('()',{':',2}))
> >
> > You might revise linear algebra if you ever wonder why there is no shorter way.
> >
> > Bruno
>
> Thank you Bruno.........good answers call for more questions:
> 1- people often warn against using “inv” function in Matlab, is it ok to use it in this case
> 2- I will definitely go back to linear algebra, but what area can I find a good explanation that addresses this section?
>
>

Also, I read on the Matlab help that xA=b -------> x=b/A = (A'/b')' , I was wondering if this could help eliminate the use of the “ inv ” function in b*subsref(inv(A),substruct('()',{':',2}))

Subject: fwd-slash division

From: Hydroman S

Date: 27 Nov, 2008 03:08:02

Message: 7 of 14

"Hydroman S" <amirgsalem@gmail.com> wrote in message <ggl267$sk3$1@fred.mathworks.com>...
> "Hydroman S" <amirgsalem@gmail.com> wrote in message <ggkp95$d4r$1@fred.mathworks.com>...
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ggkgr6$g98$1@fred.mathworks.com>...
> > > "Hydroman S" <amirgsalem@gmail.com> wrote in message <ggkdd4$1d0$1@fred.mathworks.com>...
> > >
> > > >
> > > > but the question is still holds, how to perform this division so that the output is the second ement of x only?
> > > >
> > >
> > > b*subsref(inv(A),substruct('()',{':',2}))
> > >
> > > You might revise linear algebra if you ever wonder why there is no shorter way.
> > >
> > > Bruno
> >
> > Thank you Bruno.........good answers call for more questions:
> > 1- people often warn against using “inv” function in Matlab, is it ok to use it in this case
> > 2- I will definitely go back to linear algebra, but what area can I find a good explanation that addresses this section?
> >
> >
>
>
Correction: Also, I read on the Matlab help that xA=b -------> x=b/A = (A'\b')' , I was wondering if this could help eliminate the use of the “ inv ” function in b*subsref(inv(A),substruct('()',{':',2}))

Subject: fwd-slash division

From: John D'Errico

Date: 27 Nov, 2008 03:17:03

Message: 8 of 14

"Hydroman S" <amirgsalem@gmail.com> wrote in message <ggl2ui$hf5$1@fred.mathworks.com>...

> Correction: Also, I read on the Matlab help that xA=b -------> x=b/A = (A'\b')' , I was wondering if this could help eliminate the use of the “ inv ” function in b*subsref(inv(A),substruct('()',{':',2}))

No. It does not. However, you can write it as

(b/A)*[0 1 0]'

This is cheating in a sense. Or not.

John

Subject: fwd-slash division

From: Hydroman S

Date: 27 Nov, 2008 04:55:04

Message: 9 of 14

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <ggl3ff$44v$1@fred.mathworks.com>...
> "Hydroman S" <amirgsalem@gmail.com> wrote in message <ggl2ui$hf5$1@fred.mathworks.com>...
>
> > Correction: Also, I read on the Matlab help that xA=b -------> x=b/A = (A'\b')' , I was wondering if this could help eliminate the use of the “ inv ” function in b*subsref(inv(A),substruct('()',{':',2}))
>
> No. It does not. However, you can write it as
>
> (b/A)*[0 1 0]'
>
> This is cheating in a sense. Or not.
>
> John

Thanks John.

Is it true then that there is no way arround doing the division on the full size of b & A without using " inv ". My goal was to reduce the size of b and A to get the x elment of interst.

It might also be worth it to share the reason why I'm trying to do this in case someone has any sugesstions:

I'm solving a system of the form Ax=b --------> x = b \ A

x is 6 x1 , A is 6 x 6 & b is 6 x 1.
A is also a function of x, so my solution involves a simple iteration over x = b \ A , i.e.
x(n+1) = b \ A(x(n))

I want to perform the iteration over A(5,:) , b(5,:) instead of iterating arround the whole system, the result of interst is x(5,:) . I don't want to use Newton method or fsolve for my solution.....

I really appreciate all your help and suggestions.

Subject: fwd-slash division

From: Bruno Luong

Date: 27 Nov, 2008 07:35:06

Message: 10 of 14

"Hydroman S" <amirgsalem@gmail.com> wrote in message <ggl978$1mk$1@fred.mathworks.com>...

>
> Is it true then that there is no way arround doing the division on the full size of b & A without using " inv ". My goal was to reduce the size of b and A to get the x elment of interst.

Hardly Hydroman. "/" and "\" solve linear systems. The solution is expressed as inversion matrix product RHS. The inverted matrix depends on ALL elements (c.f. co-matrix, determinat and so on) of the system matrix. You cannot truncate brutally the original matrix and/of the RHS (b) and still hope getting the solution, even a small part of it.

Bruno

Subject: fwd-slash division

From: John D'Errico

Date: 27 Nov, 2008 11:52:01

Message: 11 of 14

"Hydroman S" <amirgsalem@gmail.com> wrote in message <ggl978$1mk$1@fred.mathworks.com>...

> Is it true then that there is no way arround doing the division on the full size of b & A without using " inv ". My goal was to reduce the size of b and A to get the x elment of interst.
>
> It might also be worth it to share the reason why I'm trying to do this in case someone has any sugesstions:
>
> I'm solving a system of the form Ax=b --------> x = b \ A
>
> x is 6 x1 , A is 6 x 6 & b is 6 x 1.
> A is also a function of x, so my solution involves a simple iteration over x = b \ A , i.e.
> x(n+1) = b \ A(x(n))
>
> I want to perform the iteration over A(5,:) , b(5,:) instead of iterating arround the whole system, the result of interst is x(5,:) . I don't want to use Newton method or fsolve for my solution.....
>
> I really appreciate all your help and suggestions.

Bruno is correct, so I'll just expand a bit on what
he said, though I'll add an option if you are willing
to potentially lose some accuracy.

You are solving a linear system of equations. Unless
A has some special property (an example of such a
special property would be if A was a A block diagonal
matrix, or something morally equivalent to one) you
cannot just drop some of the information from that
linear system and solve it.

The idea that you could use only one row of A here is
equivalent to starting with a system of linear equations,
but then throwing away all of the coefficients in all of
your equations except for the coefficients that involve
the variable x(5). They call it a system of equations
for a reason. It is a system, and it must stay that way.

Newton's method or fsolve would be completely silly
here. It would be the equivalent of the use of a Mack
truck to carry a pea to Boston. Why use a nonlinear
iterative scheme to solve a linear problem, especially
when it will actually call \ internally anyway?

Having said all of that, there are ways where one
can solve a linear system for one of the variables,
doing less work than might backslash or slash does.
You are trying to solve the problem

   x*A = b

where A is 6x6, but you only care about x(5). First
of all, reorder the unknowns, so that you will only
compute the last one. (Unfortunately, what I am
doing may significantly reduce the accuracy of your
solution if A is anywhere close to being singular.)

The idea I'll use is to compute a NON-pivoted LQ
decomposition of A. So if

  A = Q*L

then we can write the problem as

  x*L = b*Q'

Here L is a lower triangular matrix. Normally, we
would finish solving the problem using

  x = (b*Q')/L

and MATLAB is smart enough to know that since L
is lower triangular, then it can use a simple scheme
(back substitution) to solve it. This last part is a
fast thing to do, only O(n^2) ops, compared to the
O(n^3) ops required for the decomposition itself.

Since you already know that information, and you
only want the last element of x, we can short circuit
the last part of the solution.

How would you do this in MATLAB? MATLAB does
not have a built-in LQ factorization. But it does have
a QR. We can use those factors for our purpose.

  format long g
  [Q,R] = qr(A');
  x6 = (b*Q(:,6))/R(6,6);

Verify that this worked in an example:

  b = randn(1,6);
  A = randn(6);

% Solve for just x(6) as above.

  [Q,R] = qr(A');
  x6 = (b*Q(:,6))/R(6,6)
x6 =
         0.125469006067183

Was it correct? Compare to the complete solution
using slash.

  (b/A)'
ans =
        -0.486154697459952
        -0.613860483571218
        -0.301297492056063
        0.0744768344805876
        -0.626607248909288
         0.125469006067182

See that the last element is the same, at least to within
most of its few digits. Had A been nearly singular, the
use of a non-pivoted QR might have been more costly.

More importantly, does this save us any time? Write it
as a function so I can best compare the time required.

function x6 = testfun(A,b)
[Q,R] = qr(A');
x6 = (b*Q(:,6))/R(6,6);

Now, do some timing comparisons:

timeit(@() testfun(A,b))
ans =
      0.000171779387440118

timeit(@() (b/A)*[0 0 0 0 0 1]')
ans =
      0.000154544676339716

Even with a multiplication at the end to pick out
only the last element of x, the slash solution is still
faster! And remember, / will be more accurate when
A is poorly conditioned. Since A is really a function
of another parameter, it could easily temporarily
become nearly singular along the way.

So, can you reduce the work required to solve a 6x6
linear system? Yes you can, but you still won't save
any time, and you will lose some stability in the
solution because we had to use the unpivoted
QR decomposition.

Buy a faster computer, go get a cup of coffee, put
your feet up on the desk, and read a good book
while you wait.

John

Subject: fwd-slash division

From: Thomas Clark

Date: 27 Nov, 2008 14:58:02

Message: 12 of 14

Hydroman,

It sounds to me like the speed of the solution is your problem, right?

I mean, it's no brain strain to solve the whole thing with the slash operator and index to get the solution element you want... So I'm assuming that you'd like just to get there faster?

Why not use embedded matlab? You can set up a quick and dirty function to solve the problem you want (providing that the sample arrays sizes are constant, eg 6x6, 6x1) and return the element you want.

I haven't used the approach for forward slash, but it's saved me up to 40% of my computational load solving Ax = b with mldivide, where A is a 3x3. It might help you too (sorry, I don't have MATLAB with me right now, or I'd check).

Look up the emlmex command...

Hope this helps!

Tom




"Hydroman S" <amirgsalem@gmail.com> wrote in message <ggkcqa$m7j$1@fred.mathworks.com>...
> Consider the solution for: xA=b, where x=b/A.
>
> A=magic(3);
> b=[1 2 3];
> x= b/A;
>
> ans =
>
> -0.03333333333333 0.46666666666667 -0.03333333333333
>
> I want to perform this fwd-slash division for only one element of x, say x(:,2)
>
> So I tried:
>
> b(:,2)/A(2,2)
> but it gave me
> 0 0 0.22222222222222
>
> Can someone tell what’s wrong in what I’m doing?

Subject: fwd-slash division

From: Hydroman S

Date: 27 Nov, 2008 17:57:02

Message: 13 of 14

@ Bruno, John and Tom: Thank you so much for the feedback and the useful discussion. Very insightful and helpful.

So the best option for me then is to
- iterate x(n+1) = b / A(x(n)) until | x(n+1)-x(n) | is small (this is done for each value of x )
- otherwise let x(n) = x(n+1) and iterate until convergence.

In my mind, the problem then becomes: The convergence (small value for | x(n+1)-x(n) | ) will depend on every value of the x (1 x 6) vector instead of the value of interest only, which is x(:,5).

So by thinking that I could work on an element of x (i.e. x(:,5)) separately, I thought that the converge criteria would be less stringent for the x (:,5) element level than using the whole row vector x

Subject: fwd-slash division

From: Thomas Clark

Date: 28 Nov, 2008 17:58:02

Message: 14 of 14

I'm reluctant to point out my own stupidity, but for the sake of scientific accuracy here goes...

In response to my own code above, I've just re-tested and found that I was talking out of my **** - I'd accidentally benchmarked against the wrong thing, so in reality found only a fractional improvement by wrapping mldivide into an embedded function. Presumably TMW have optimised it already. I suppose in your case you might get a little improvement... but perhaps not the amount I'd suggested.

Sorry!

Tom Clark




"Thomas Clark" <t.clark@remove.spamcantab.net> wrote in message <ggmchq$s3p$1@fred.mathworks.com>...
> Hydroman,
>
> It sounds to me like the speed of the solution is your problem, right?
>
> I mean, it's no brain strain to solve the whole thing with the slash operator and index to get the solution element you want... So I'm assuming that you'd like just to get there faster?
>
> Why not use embedded matlab? You can set up a quick and dirty function to solve the problem you want (providing that the sample arrays sizes are constant, eg 6x6, 6x1) and return the element you want.
>
> I haven't used the approach for forward slash, but it's saved me up to 40% of my computational load solving Ax = b with mldivide, where A is a 3x3. It might help you too (sorry, I don't have MATLAB with me right now, or I'd check).
>
> Look up the emlmex command...
>
> Hope this helps!
>
> Tom
>
>
>
>
> "Hydroman S" <amirgsalem@gmail.com> wrote in message <ggkcqa$m7j$1@fred.mathworks.com>...
> > Consider the solution for: xA=b, where x=b/A.
> >
> > A=magic(3);
> > b=[1 2 3];
> > x= b/A;
> >
> > ans =
> >
> > -0.03333333333333 0.46666666666667 -0.03333333333333
> >
> > I want to perform this fwd-slash division for only one element of x, say x(:,2)
> >
> > So I tried:
> >
> > b(:,2)/A(2,2)
> > but it gave me
> > 0 0 0.22222222222222
> >
> > Can someone tell what’s wrong in what I’m doing?

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us