double indexing

127 ビュー (過去 30 日間)
Frank
Frank 2012 年 1 月 12 日
コメント済み: Petter 2023 年 7 月 11 日
Hello,
is it possible to do double or multiple indexing in matlab? E.g.
>> v = [1,2,5,7,9]
v =
1 2 5 7 9
>> v(1:4)
ans =
1 2 5 7
>> v(1:4)(2:3)
ans =
2 5
The first index create a new vector and the second index creates a new vector out of the newly created one. It is common in other languages, and it helps to avoid defining temp variables.
Cheers, Frank

採用された回答

Walter Roberson
Walter Roberson 2012 年 1 月 12 日
The closest you can get is
subsref(V(1:4), struct('type', '()', 'subs', 2:3)))
You can follow {} referencing by () referencing, {}(), but you cannot use ()() or (){} or ().{} or ().(), and you cannot use function(){} or function()() or function().field
  2 件のコメント
Frank
Frank 2012 年 1 月 12 日
thanks. Then I prefer to use a temp var instead. :)
Petter
Petter 2023 年 7 月 11 日
"You can follow {} referencing by () referencing, {}()"
This one line made my day so much easier, greatly appreciated. Thumbs up, gold star.

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

その他の回答 (2 件)

mklcst mklcst
mklcst mklcst 2014 年 1 月 23 日
I think it could be very useful to have a short way to perform double indexing.

Jos (10584)
Jos (10584) 2014 年 1 月 23 日
If the indices are stored in variables, this is trivial!
V = [1,2,5,7,9]
ii = 1:4
jj = [2 3]
out = V(ii(jj))
  1 件のコメント
lee eugene
lee eugene 2019 年 8 月 2 日
However, if the two index do not have the same starting indexing, there would be something wrong. For example, i would like to select index [2,4] from [1,2,5,7,9] at first, and then select index [1,2]. Then it would be [2,5,7] at first and [2,5] in the end.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by