Mixing subscripts with linear indices

23 visualizaciones (últimos 30 días)
Matt J
Matt J el 29 de Nov. de 2025
Editada: Paul el 1 de Dic. de 2025
Generally speaking, I have found that it has always been possible to mix subscript and linear array indexing, as long as the indexing syntax is in the form,
A(subscript_1,subscript_2,...,subscript_n, linear_index).
For example, given,
A = reshape((1:24)*3, [4, 3, 2]);
all of the following are valid ways of indexing the final element in A,
A(4,3,2)
ans = 72
A(4,6)
ans = 72
A(24)
ans = 72
However, I haven't been able to find documentation of this, except of course for the trivial case where n=0. Is it officially supported behavior?
  1 comentario
Paul
Paul el 29 de Nov. de 2025
Same example as above, with other cases
A = reshape((1:24)*3, [4, 3, 2])
A =
A(:,:,1) = 3 15 27 6 18 30 9 21 33 12 24 36 A(:,:,2) = 39 51 63 42 54 66 45 57 69 48 60 72
The linear index can be a vector
A(:,[3 5])
ans = 4×2
27 51 30 54 33 57 36 60
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Or a colon
A(2,:)
ans = 1×6
6 18 30 42 54 66
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A(:,:)
ans = 4×6
3 15 27 39 51 63 6 18 30 42 54 66 9 21 33 45 57 69 12 24 36 48 60 72
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Or even an array, which gets stretched into a vector index
A(:,[1:2;1:2])
ans = 4×4
3 3 15 15 6 6 18 18 9 9 21 21 12 12 24 24
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I too can't find relevant documentation.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 29 de Nov. de 2025
Editada: Stephen23 el 29 de Nov. de 2025
When fewer subscripts are provided than non-singleton dimensions then all trailing dimensions are "folded" into the last subscript. This is explicitly described in the documentation section entitled "Indexing with Fewer Subscripts Than Array Dimensions":
Loren Shure also wrote: "Indexing with fewer indices than dimensions If the final dimension i<N, the right-hand dimensions collapse into the final dimension."
Note that linear indexing is really just a side-effect of this. An earlier discussion on this:
  6 comentarios
Stephen23
Stephen23 el 29 de Nov. de 2025
"AFAICT, the information on the page linked by @Stephen23 entitled "Detailed Rules for Array Indexing" was only added to the documentation in R2025a"
Correct. Previously the only location that I recall was Loren's blog (from 2006).
Paul
Paul el 1 de Dic. de 2025
Editada: Paul el 1 de Dic. de 2025
I'm inordinately annoyed that "Detailed Rules for Array Indexing" is only considered to be a "Related Topic" to "Array Indexing." Seems like "detailed rules" would actually be fundamental to the discussion of array indexing. If the doc authors thought the detailed rules would add too much information to the Array Indexing page, then the detailed rules should be linked directly from the main discussion on that page, instead of or in addition to being a "Related Topic."
Also, it's rather disappointing that such a core functionality for a Matrix Laboratory is only showing up in the doc now, about 20 years after it was (informally) published, and I suspect it's been part of Matlab since the introduction of N-D arrays.
And if they were going to add the Detailed Rules, then why not add all of them? In addition to the example I showed above, Loren's blog shows:
" Indexing with one array C = A(B) produces output the size of B unless both A and B are vectors."
I think I knew about this, but I don't see it on either of the two doc pages in this discussion.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by