double indexing
96 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Walter Roberson
le 12 Jan 2012
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 commentaires
Petter
le 11 Juil 2023
"You can follow {} referencing by () referencing, {}()"
This one line made my day so much easier, greatly appreciated. Thumbs up, gold star.
Plus de réponses (2)
mklcst mklcst
le 23 Jan 2014
I think it could be very useful to have a short way to perform double indexing.
0 commentaires
Jos (10584)
le 23 Jan 2014
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 commentaire
lee eugene
le 2 Août 2019
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.
Voir également
Catégories
En savoir plus sur Function Creation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!