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
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?
采纳的回答
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
2023-6-5
Remember that [] is the list-building operator, not just a different way of writing ()
更多回答(2 个)
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
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
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 -
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!