A unknown bug on matrix

조회 수: 1 (최근 30일)
Ziwen Gu
Ziwen Gu 2023년 6월 5일
댓글: Ziwen Gu 2023년 6월 5일
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.
  댓글 수: 1
Ziwen Gu
Ziwen Gu 2023년 6월 5일
Thanks for your quick reply, I got it now :)

댓글을 달려면 로그인하십시오.


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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by