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

2 件のコメント

Katharina Wellstein
Katharina Wellstein 2024 年 12 月 20 日
編集済み: Walter Roberson 2024 年 12 月 20 日
A simple way to do this would be by using intersect, a function that finds commonalities between different arrays.
In this case it would be:
[value, position] = intersect(v(1:4), v(2:3))
For more complicated things like indexing into an array (or matrix) with different indices, the same thing can be used. E.g. if I want to find numbers that are higher than 5 and numbers that are even in x, I can do the following:
x = randi(randi(10,10,1);
bigIdx = find(x>5);
evenIdx = ~mod(x,2);
x(intersect(bigIdx,evenIdx);
Walter Roberson
Walter Roberson 2024 年 12 月 20 日
The hypothetical v(1:4)(2:3) is intended to select elements 1 through 4 of v, and then select elements 2 to 3 of that.
Suppose we had instead asked for v(2:4)(2:3), then that would be elements 2 to 3 out of elements 2 to 4, so the end result would be v(3:4) .
[value, position] = intersect(v(2:4), v(2:3))
would give back v(2:3) instead, at best.
If we had
v = [1,5,5,7,9]
then [value, position] = intersect(v(1:4), v(2:3)) would be interesect([1 5 5 7], [5 5]) which is going to return [5] rather than [5 5] because the return values of intersect() are sorted and unique by default.
Overall, intersect() is just the wrong thing to use here.

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

 採用された回答

Walter Roberson
Walter Roberson 2012 年 1 月 12 日

2 投票

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

3 件のコメント

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.
Walter Roberson
Walter Roberson 2024 年 12 月 20 日
In the time since the above Answer was posted, Mathworks has since permitted function().field

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

その他の回答 (2 件)

mklcst mklcst
mklcst mklcst 2014 年 1 月 23 日

3 投票

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

3 投票

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))

2 件のコメント

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.
DGM
DGM 2024 年 12 月 20 日
編集済み: DGM 2024 年 12 月 20 日
That's what the result is supposed to be.
That said, there's still room for problems if the index vectors are programmatically generated. We'd have to safeguard against out-of-range indices. Consider one possibility:
% a vector
V = [1,2,5,7,9];
% potentially out-of-range indices
ii = [1:4 7];
jj = [2 3 5];
% one option might be to simply
% discard invalid indices
ii = intersect(ii,1:numel(V))
ii = 1×4
1 2 3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
jj = intersect(jj,1:numel(ii))
jj = 1×2
2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% so it still works without indexing errors
out = V(ii(jj))
out = 1×2
2 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I'm sure there are other ways one might choose to handle it though. This might not be okay if you're not expecting to a different number of elements than what you requested. In that case, it might be better to just throw an error.

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

カテゴリ

ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索

タグ

質問済み:

2012 年 1 月 12 日

編集済み:

DGM
2024 年 12 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by