agus
agus

Reputation: 33

Right division by row vector in MATLAB

I'm right dividing a 20x60 matrix called A, by a row 1x60 vector called B. So C = A/B, where C is a 20x1 vector.

What is MATLAB doing in A/B? I found the answer in mrdivide

If A is a rectangular m-by-n matrix with m ~= n, and B is a matrix with n columns, then x = B/A returns a least-squares solution of the system of equations x*A = B.

but when I try C*B or B*C I get a dimension error, why?

Upvotes: 3

Views: 247

Answers (1)

Pablo Jeken Rico
Pablo Jeken Rico

Reputation: 589

As you said, entering the command C = A/B you are solving C in the equation C * B = A. If you have the following system (2 instead of 20 and 3 instead of 60, to keep it simple).

To the first question, Matlab looks at the problem and tries to solve it. In some cases the system won't have a solution. In that cases Matlab computes one of the combinations, that solve the most equations (as shown in the example on the mathworks page).

To the second question regarding why you get an dimension error:

b = [b1 b2 b3]

A = [a11 a12 a13]
    [a21 a22 a23]

C = [c1]
    [c2]

MatLab will handle what you are trying to do with vectors, because it automatically recognizes the C*B as an outer product.

The products of B*C for matrices won't work, because the matlab automatism will not take the possibility of the outer product into account. If you want to perform the calculus you will have to use the kronecker product:

kron(C,B)

Output for this example:

=[c1*b1 c1*b2 c1*b3]
 [c2*b1 c2*b2 c2*b3]

I hope this helps you.

Cheers, Pablo

Upvotes: 1

Related Questions