A unknown bug on matrix

1 次查看(过去 30 天)
Follow the fig, R and S are matrix of size(4225*4225), delta, sigma,delta_gamma are scale parameter.
When they minus, the size should still be 4225*4225. However when there is a space behind '-' , size becoms 4225*8450.
  2 个评论
Stephen23
Stephen23 2023-6-5
"A unknown bug on matrix"
The more important question is: why are you using completely superfluous square brackets, when you are not concatenating anything together?
Square brackets are a concatenation operator.
But apparently you are not intending to concatenate anything together....
So why use square brackets?
Ziwen Gu
Ziwen Gu 2023-6-5
Thanks for all of your quick reply... In fact I am concatenating matrix to a big matrix, but find the size is not matched. The fig is where the problem arises when I debug.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2023-6-5
Consider:
A = [3 2 1 0 -1 -2 -3]
Are you expecting the result to be [3, 2, 1, (0-1-2-3)] --> [3, 2, 1, -6] a vector of 4 elements? Or are you expecting [3, 2, 1, 0, -1, -2, -3] a vector of 7 elements?
Inside [] and {}, if you have VALUE1 -VALUE2 then that is treated as [VALUE1, uminus(VALUE2)] where uminus is unary minus. Likewise inside [] and {}, if you have VALUE1 +VALUE2 then it is treated as [VALUE1, uplus(VALUE2)] where uplus, + is unary plus.
If you have VALUE1-VALUE2 or VALUE1- VALUE2 or VALUE1 - VALUE2 then those are all treated as minus(VALUE1, VALUE2) but VALUE1 -VALUE2 is unary operator not subtraction.
I suspect that your expectation is that inside [] and {} that VALUE1 -CONSTANT is treated as [VALUE1, negative_CONSTANT] and that your expectation is that VALUE1 -non_constant_EXPRESSION is treated as minus(VALUE1, non_constant_EXPRESSION) but as far as the MATLAB parser is concerned, [VALUE1 -CONSTANT] and [VALUE1 -non_constant_expression] have the same form.
  1 个评论
Walter Roberson
Walter Roberson 2023-6-5
Remember that [] is the list-building operator, not just a different way of writing ()

请先登录,再进行评论。

更多回答(2 个)

KSSV
KSSV 2023-6-5
This is not bug....don't use the sqaure braces.
If you are using square braces, than space is considered as a different element.

Satwik
Satwik 2023-6-5
The reason for this is that on adding space before '-', MATLAB interprets it as concatenation instead of subtraction. That is the reason the size is doubled. The second line is negating the values of delta*sigma*S and concatenating with R/delta_gamma. So, you cannot give a space before the '-'.
  2 个评论
Stephen23
Stephen23 2023-6-5
"The reason for this is that on adding space before '-', MATLAB interprets it as concatenation instead of subtraction."
In general this is not correct. In some specific situations within the concatenation operators (i.e. square brackets) this might be the effect observed. But a more accurate description is that unary negation has a higher priority than binary subtraction, just as the MATLAB documentation explains:
"So, you cannot give a space before the '-'."
Really? It works for me:
3 - 1 % a space before the -
ans = 2
Ziwen Gu
Ziwen Gu 2023-6-5
Thanks for your reply :)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by