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

2 ビュー (過去 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

Community Treasure Hunt

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

Start Hunting!

Translated by