Where is the Documentation for Indexing on the Left Hand Side of an Assignment?

11 ビュー (過去 30 日間)
Paul
Paul 2022 年 9 月 11 日
編集済み: Paul 2022 年 9 月 12 日
I recently ran into something like this:
x = [1 2 3];
x([1 2 1]) = [10 20 30]
x = 1×3
30 20 3
So it looks like the second index of 1 on the LHS takes precedence over the first. I hunted through the documentation (searching on "assignment" was particularly fruitless) and I couldn't find any documentation discussing how indexing works on the LHS of an assignment in general, much less the rule for this particular use case.
Can anyone link to relevant doc page(s)? I feel like I've seen this topic in the doc before, but for the life of me can't find it now.
  2 件のコメント
Chris
Chris 2022 年 9 月 12 日
編集済み: Bruno Luong 2022 年 9 月 12 日
I mean...
Slicing the array up with repeated elements makes sense on the right side, perhaps, but why would that happen on the left side? Matlab is probably just following the index order. Working through the array, it first sets x(1) to 10 (index 1), then ends by setting x(1) to 30 (index 3).
Paul
Paul 2022 年 9 月 12 日
編集済み: Paul 2022 年 9 月 12 日
Both of those links talk only about how data is extracted from an array with indexing in an expression. Neither talk about how data is assigned into an array with indexing. For me, "Matlab is probably" suggests it's worthy of being documented.
I don't know why that would happen on the LHS, but it's allowed so maybe there is some need/use for that feature.
Stephen's answer links to two blog posts that are exactly on point, so there must be some interest somewhere.

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

採用された回答

Stephen23
Stephen23 2022 年 9 月 12 日
編集済み: Stephen23 2022 年 9 月 12 日
  1 件のコメント
Paul
Paul 2022 年 9 月 12 日
Thanks for the links. However, I don't think blog posts are considered normative. IMO, the doc should cover indexing on the LHS (and I thought it did, at least in the past).

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by