A unknown bug on matrix

1 view (last 30 days)
Ziwen Gu
Ziwen Gu on 5 Jun 2023
Commented: Ziwen Gu on 5 Jun 2023
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 Comments
Stephen23
Stephen23 on 5 Jun 2023
"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 on 5 Jun 2023
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.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 5 Jun 2023
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 Comment
Walter Roberson
Walter Roberson on 5 Jun 2023
Remember that [] is the list-building operator, not just a different way of writing ()

Sign in to comment.

More Answers (2)

KSSV
KSSV on 5 Jun 2023
This is not bug....don't use the sqaure braces.
If you are using square braces, than space is considered as a different element.
  1 Comment
Ziwen Gu
Ziwen Gu on 5 Jun 2023
Thanks for your quick reply, I got it now :)

Sign in to comment.


Satwik
Satwik on 5 Jun 2023
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 Comments
Stephen23
Stephen23 on 5 Jun 2023
"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 on 5 Jun 2023
Thanks for your reply :)

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!