What is the meaning of x=x(:).'?

203 ビュー (過去 30 日間)
moh mor
moh mor 2023 年 4 月 14 日
回答済み: Image Analyst 2023 年 4 月 14 日
What is the meaning of x=x(:).' in MATLAB?
  1 件のコメント
Stephen23
Stephen23 2023 年 4 月 14 日

サインインしてコメントする。

回答 (4 件)

Cris LaPierre
Cris LaPierre 2023 年 4 月 14 日
x=magic(5)
x = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
x(:)
ans = 25×1
17 23 4 10 11 24 5 6 12 18
x=x(:).'
x = 1×25
17 23 4 10 11 24 5 6 12 18 1 7 13 19 25 8 14 20 21 2 15 16 22 3 9
  3 件のコメント
Stephen23
Stephen23 2023 年 4 月 14 日
"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
Cris LaPierre 2023 年 4 月 14 日
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
Adam Danz 2023 年 4 月 14 日
移動済み: Image Analyst 2023 年 4 月 14 日
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.

Walter Roberson
Walter Roberson 2023 年 4 月 14 日
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...

Image Analyst
Image Analyst 2023 年 4 月 14 日
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).

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by