What is the meaning of x=x(:).'?
189 views (last 30 days)
Show older comments
What is the meaning of x=x(:).' in MATLAB?
1 Comment
Stephen23
on 14 Apr 2023
"and the transpose (')"
' is complex conjugate transpose: https://www.mathworks.com/help/matlab/ref/ctranspose.html
.' is transpose: https://www.mathworks.com/help/matlab/ref/transpose.html
Answers (4)
Cris LaPierre
on 14 Apr 2023
x=magic(5)
x(:)
x=x(:).'
3 Comments
Stephen23
on 14 Apr 2023
"I think this just tell me about x=x(:)' not x=x(:).'"
Chris LaPierre's answer uses transpose, just like you asked about:
"What is sign of "."?"
Your question is unclear: a dot character does not have a "sign" (and MATLAB does not have a "." operator).
Cris LaPierre
on 14 Apr 2023
I figured it would be easier to show what it does than try to explain it. Now that you have clarified your question, you can find it answered here.
Adam Danz
on 14 Apr 2023
Moved: Image Analyst
on 14 Apr 2023
This ensures x is a row vector. It is useful when you are unsure of the size or shape of an input but you need it to be a row vector.
x(:) forces x to be a column vector and the transpose (') makes the column vector a row vector, as Cris' answer shows.
The reason you can't just do the transpose x' is because x might already be a row vector or a multidimensional array.
0 Comments
Walter Roberson
on 14 Apr 2023
In current releases,
x(:).'
for numeric values has two different meanings.
In the case where x is either real-valued, or is complex-valued with at least one non-zero imaginary component, then x(:).' is the same as
reshape(x, 1, [])
and the resulting value will share its data pointer with x.
In the case where x is complex-valued with all of its imaginary components being zero, then x(:).' is the same as
reshape(real(x), 1, [])
and the resulting value will not share its data pointer with x and will not be marked as complex.
If I recall correctly, there was a recent series of releases in which for complex values with at least one non-zero imaginary part, the result of x(:).' did not share data pointers with x.
Mathworks dithers over whether (:) and (:).' should be treated as "syntax" or as "operators". The general rule for "syntax" is that data pointers should be preserved when it makes sense to do so, whereas the general rule for "operators" is that the result is logically a different entity than the original and so should not share data pointers. Somehow over the years that evolved into a situation where (:) and (:).' applied to real-valued arrays is "syntax" but when applied to complex-valued arrays is "operator". Except now (R2023a at least) if the complex-valued array has a non-zero imaginary part then it is "syntax" after-all...
The situation gets more complicated when x is an object. The model for (:) is that it is an indexing operator, involving calling the object subsref() (or, more recently, the specific () indexing operator) with index ':', and the model for .' after that is that it involves calling transpose() . That is not what happens for the fundamental datatypes like double: (:) for the numeric types in practice is implemented in terms of pure reshape() unless the object has all-zero complex component in which case reshape on real(x) . The difference between subsref() with ':' index and reshape() is that reshape() is defined as preserving data pointers...
0 Comments
Image Analyst
on 14 Apr 2023
x=x(:).' makes it a row vector. Taking it a step at a time, using ":" in MATLAB generally means "all" but if you do (:) it will take all elements of the array, no matter what dimensions it has 1-D, 2-D or 30-D, and strings all the numbers into a single column vector. Then the apostrophe takes that column vector and transposes it so that now it becomes a row vector. It's essentially the same as doing this
x = reshape(x, 1, []);
which is an alternative way of "reshaping" or turning a multidimensional array into a row vector. The second argument is 1 meaning the reshaped array has 1 row. The third argument, [], is the number of columns and since I passed it [] it tells it to figure out on its own how many columns there needs to be (so I don't have to compute it myself).
0 Comments
See Also
Categories
Find more on Logical 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!