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

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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2025년 11월 29일
편집: Stephen23 2025년 11월 29일

1 개 추천

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

Matt J
Matt J 2025년 11월 29일
Yep, that's it. Well, it's good to confirm that it's not going away...
Stephen23
Stephen23 2025년 11월 29일
One could equally define this as applying to all trailing dimensions, singleton or not. The product of countless singletons** is trivially equal to one, which is completely consistent with this definition.
Paul
Paul 2025년 11월 29일
AFAICT, the information on the page linked by @Stephen23 entitled "Detailed Rules for Array Indexing" was only added to the documentation in R2025a. Compare the items (particularly the last) under Topics->Array Indexing in Matrices and Arrays - 2024B to the same section in the current page Matrices and Arrays.
I searched Detailed Rules in the 2024B doc and found nothing, so it's either not there or is going by different title in an obscure location.
Taking an example from Detailed Rules for Array Indexing
A = magic(5);
Mrow = [1 4; 2 3];
Mcol = [1 5; 2 3];
id1 = Mrow(:);
id2 = Mcol(:);
B = A(id1,id2);
The doc page does not explicitly show the same can be achieved w/o first forming id1 and id2
B1 = A(Mrow,Mcol);
isequal(B,B1)
ans = logical
1
The doc page does state: "While MATLAB supports subscript matrices and multidimensional arrays as indices, you can convert these indices to subscript vectors depending on your use cases."
But I don't see anything on exactly how it handles subscript matrices and multi-dimensional arrays.
Stephen23
Stephen23 2025년 11월 29일
"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 2025년 12월 1일
편집: Paul 2025년 12월 1일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

질문:

2025년 11월 29일

편집:

2025년 12월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by