What is the difference between [v' 1] and [v', 1]?

조회 수: 3 (최근 30일)
Thanh Pham
Thanh Pham 2021년 10월 15일
답변: Walter Roberson 2021년 10월 15일
I come across these two notations and still cannot distinguish them.
v' is the transpose of v. The first notation [v' 1] will be transpose of v' with column of 1 at the end.
Is [v', 1] related to matrix indexing? Please help.

채택된 답변

KSSV
KSSV 2021년 10월 15일
v = rand(3,1) ;
[v' 1]
ans = 1×4
0.5957 0.0098 0.4734 1.0000
[v',1]
ans = 1×4
0.5957 0.0098 0.4734 1.0000
That is nothing but vector v is increased by one element at the end and the value is 1.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 10월 15일
v = ((1:3) -(2:4)*1i).'
v =
1.0000 - 2.0000i 2.0000 - 3.0000i 3.0000 - 4.0000i
[v' 1]
ans =
1.0000 + 2.0000i 2.0000 + 3.0000i 3.0000 + 4.0000i 1.0000 + 0.0000i
[v', 1]
ans =
1.0000 + 2.0000i 2.0000 + 3.0000i 3.0000 + 4.0000i 1.0000 + 0.0000i
You can see that even for complex numbers, [v' 1] and [v', 1] produce the same results. This is not an accident.
The difference between [v' 1] and [v', 1] is that the version with the comma reduces the risk of values accidentally being treated as subtraction:
[v' -1]
ans =
1.0000 + 2.0000i 2.0000 + 3.0000i 3.0000 + 4.0000i -1.0000 + 0.0000i
[v', 1]
ans =
1.0000 + 2.0000i 2.0000 + 3.0000i 3.0000 + 4.0000i 1.0000 + 0.0000i
[v' - 1]
ans =
0.0000 + 2.0000i 1.0000 + 3.0000i 2.0000 + 4.0000i
[v', - 1]
ans =
1.0000 + 2.0000i 2.0000 + 3.0000i 3.0000 + 4.0000i -1.0000 + 0.0000i
Notice that the [v' - 1] form was treated as subtraction of 1, but [v' -1] and [v', -1] and [v', - 1] forms treated the -1 or - 1 as a negative 1 rather than a subtraction.
The rule is:
If the last thing you saw was a delimiter or a delimiter followed by whitespace, then + and - following are the "unary plus" and "unary minus" operators, no matter whether they are followed by whitespace or not.
If the last thing you saw was an expression without delimiter, or whitespace after an expression without delimiter, then + followed by whitespace is addition, and - followed by whitespace is subtraction, but + followed without whitespace is unary plus, and - followed without whitespace is unary minus.
% U U S A
[1 +1 -1 1 - 1 + 2]
ans = 1×4
1 1 -1 2
U here indicating unary operator, S indicating subtraction, A indicating addition. Compare to
[1, +1, -1, 1, - 1, + 2]
ans = 1×6
1 1 -1 1 -1 2
So the comma help prevent accidental interpretation as subtraction or addition

카테고리

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