Error: Matrix dimensions must agree

4 views (last 30 days)
Joshua Phillips
Joshua Phillips on 11 Oct 2015
Edited: Stephen23 on 12 Oct 2015
Hi there,
I am having issues using the .\ operator to ldivide a matrix MxN by a vector with N rows.
Details of the Matrix:
A = 9898 x 9999
rhs = 9999 x 1
Cnew = A.\rhs
Not sure where I'm going wrong here, I've tried various combinations to see where the error is but can't seem to make it happen.
Thanks in advance

Answers (1)

Stephen23
Stephen23 on 11 Oct 2015
Edited: Stephen23 on 12 Oct 2015
It depends on what you are trying to do.
First you should learn about array vs. matrix operations. Knowing the difference and knowing when to use them is critical when using MATLAB!
ldivide .\ is an element-wise (or array) operator, which operates on same-sized arrays, and it divides each element of one array by the corresponding element from the other array.
mldivide \ is a matrix operation, where the number of rows of the two matrices must be the same, and it solves the linear equations defined by those matrices.
They look similar, but that period is very significant!
You have not told us if you are trying to solve linear equations, or if you are trying to simply divide all of the values. So here are both solutions. If you want to get the least-squares solution:
A\rhs % returns a least-squares solution to the system of equations A*x = rhs.
If you want to divide every element in rhs by an element of A, expanding rhs to match the dimensions of A, then try this:
bsxfun(@ldivide,A,rhs)

Products

Community Treasure Hunt

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

Start Hunting!