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

1 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2023-4-19
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 个评论
Walter Roberson
Walter Roberson 2023-4-19
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by