backslash(\) is through error in 2013a but not to 2018b

1 view (last 30 days)
the below syntax is working in matlab 2018b
s = [1 2 3 4; 5 6 7 9; 1 1 1 1 ; 1 1 1 1];
b=[1;1;1;1];
d = s.\b
%%result
d =
1.0000 0.5000 0.3333 0.2500
0.2000 0.1667 0.1429 0.1111
1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000
but in 2013a is throug "Matrix dimensions must agree."
may i know why?
Thanks in advance
Murugan

Accepted Answer

Walter Roberson
Walter Roberson on 19 Apr 2023
You did not use the \ operator: you used the .\ operator.
A.\B is the same as B./A -- element-wise division but with the order of operands reversed from the usual element-wise division.
The .\ operator is obscure, and I have never found a reason to use it other than for testing.
The reason the operator works on newer versions but fails on older versions is that you are doing element-by-element division between two arrays the same size. R2015b added the feature of "implicit expansion": for the element-by-element binary operators between two arrays, if the two arrays are the same size except that one might have size 1 in a dimension that the other is not 1, then the array with size 1 for the dimension will be automatically replicated to be the same size as the other.
Now, if you had used d = s\b with the \ operator instead of the .\ operator, then you would have had proper array sizes for the matrix-left-divide operator even back in R2013 time frame.
  3 Comments
Walter Roberson
Walter Roberson on 19 Apr 2023
What calculation are you trying to perform?
The last two rows of s are the same, so s cannot possibly be full rank, so if you were hoping to do least-squared fitting or trying to solve s*x = b then you will not be able to do so.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!