Why does accessing a multi-dimensional array with fewer indices than its total dimensions not result in an error in MATLAB R2022b?

6 次查看(过去 30 天)
I constructed a multi-dimensional array in MATLAB and called it with fewer indices. I was expecting to get an error message for the missing index, but the script was executed without any error message. Here's an example:
>> size(Pcc)
ans =
    5 6 5 6 6
>> Pcc(1,1,1,1)
ans =
    0.7854
Is this a bug?

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2023-11-6
If you access a multi-dimensional array in MATLAB with fewer indexes than its total dimensions, the array is treated as if it had only the dimensions you specified. The remaining dimensions are merged into a single dimension. In your case, the "Pcc" array has dimensions 5x6x5x6x6, and when you accessed it using only 4 indices, MATLAB collapsed the last two dimensions, 6x6, into a single dimension 36. As a result, when you execute the command "Pcc(1,1,1,1)", MATLAB accesses the "Pcc" array as if it were reshaped to "Pcc(5,6,5,36)" and returned the corresponding value at that specific location.
Here's an example to illustrate this behavior with a simpler 3-dimensional array:
>> A = rand(3, 4, 2)
>> A(2, 3)
ans =
    0.5469
>> B = reshape( A, 3, 8);
>> B(2, 3)
ans =
    0.5469
In this example, "A(2, 3)" has the same value with "B(2, 3)" where "B" is the same as array "A" reshaped to "A(3, 8)" where the last two dimensions are merged into a single dimension. 
Overall, this behavior is not a bug but rather a built-in feature of MATLAB. If you want to access specific elements in a multi-dimensional array, it is recommended to provide the appropriate number of indices for each dimension. 
  1 个评论
Bruno Luong
Bruno Luong 2023-9-2
编辑:Bruno Luong 2023-9-2
This answer not the question posted but rather a different one "Why does accessing a multi-dimensional array with more indices than its total dimensions not result in an error in MATLAB R2022b?"
The correct answer is @James Tursa answer bellow

请先登录,再进行评论。

更多回答(1 个)

James Tursa
James Tursa 2023-9-1
编辑:James Tursa 2023-9-1
The accepted answer is misleading. When you access a multi-dimensional array with fewer indexes than its total dimensions, MATLAB treats the array as if it had only the dimensions you used, with the last dimension being all the actual remaining dimensions bunched into one dimension in the same memory order. E.g.,
A = reshape(1:24,2,3,4)
A =
A(:,:,1) = 1 3 5 2 4 6 A(:,:,2) = 7 9 11 8 10 12 A(:,:,3) = 13 15 17 14 16 18 A(:,:,4) = 19 21 23 20 22 24
A(2,3)
ans = 6
A(2,5)
ans = 10
So, why did that last A(2,5) work? Clearly 5 is greater than the actual 2nd dimension value 3. It worked because the (2,3,4) dimensions of A are treated as if the dimensions are (2,3*4) = (2,12) when accessing with only two indexes. The last part is simply accessed in memory order. It is as if you did a temporary reshape(A,2,12) first:
reshape(A,2,3*4)
ans = 2×12
1 3 5 7 9 11 13 15 17 19 21 23 2 4 6 8 10 12 14 16 18 20 22 24
And from this you can see that the A(2,5) element is indeed 10 when A is treated as a temporarily reshaped 2D matrix.
Bottom line is when you access a multi-dimensional array using fewer indexes than there are dimensions, MATLAB treats the array as a reshaped array using the number of dimensions that matches the number of indexes you used with the last dimension being the product of all trailing dimensions. Another example:
A = reshape(1:36,3,2,3,2)
A =
A(:,:,1,1) = 1 4 2 5 3 6 A(:,:,2,1) = 7 10 8 11 9 12 A(:,:,3,1) = 13 16 14 17 15 18 A(:,:,1,2) = 19 22 20 23 21 24 A(:,:,2,2) = 25 28 26 29 27 30 A(:,:,3,2) = 31 34 32 35 33 36
A(2,10)
ans = 29
reshape(A,3,2*3*2)
ans = 3×12
1 4 7 10 13 16 19 22 25 28 31 34 2 5 8 11 14 17 20 23 26 29 32 35 3 6 9 12 15 18 21 24 27 30 33 36
Again, you can see that the A(2,10) spot matches what you would expect from the reshaped matrix.
  2 个评论
Stephen23
Stephen23 2023-9-2
编辑:Stephen23 2023-9-2
Note that the indexing documentation does not mention this behavior, but it is explained here:
An interesting corollary of this is that linear indexing is just an edge-case of subscript indexing.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by