Accessing Elements in a 3D matrix using Linear Indexing ?
显示 更早的评论
Hello,
I would like to know if it is possible to access the individual element of a 3D matrix of size M*N*P using linear indexing?
采纳的回答
更多回答(2 个)
Image Analyst
2020-8-11
Yes, it's possible but you'd need 3 dimensions for the linear array, not 2. So not M-by-N but rows-by-columns-by-slice.
[rows, columns, slices] = size(your3DArray);
mask = whatever; % Needs to be a "rows by columns by slices" 3-D array.
your3DArray(mask) = whateverYouWant;
5 个评论
Image Analyst
2020-8-11
You just want to know "how MatLab able to use linear indexing to calculate the location of an element in a 3D matrix." and it's in column major order, basically by iterating the left most index first, then the second index, then finally the third index the slowest. So if you imagine a 3-D matrix as a stack of elements in a rectangular block/slab, it will first to the top slice going down each row in the left column, then moving over to the next column and doing all the rows there, then to the next column and so on until that first slice is "used up". Then it goes to the next slice, and repeats the process. So if you had a 3 row by 4 column by 2 slice array, like this
>> numElements = 3*4*2;
>> A = reshape(1:numElements, 3,4,2)
A(:,:,1) =
1 4 7 10
2 5 8 11
3 6 9 12
A(:,:,2) =
13 16 19 22
14 17 20 23
15 18 21 24
That basically shows you how the linear indexing goes. So if you wanted A(20), it would be A(2, 3, 2). In other words, it's at row 2, column 3 of slice #2.
Venkata Khambhammettu
2020-8-11
Image Analyst
2020-8-11
I told you the algorithm. As you go from linear index 1 to 2 to 3 to 4, etc. it starts with slice 1 and goes down rows first (see the left column of A(:,:,1)) and then moves over to the second column - see where it says 4,5,6 in column 2 of A(:,:,1)? Then it moves over to the 3rd column, and finally the 4th/last column. Then it repeats that order for the second slice so that's how it goes from 13 to 24. Basically it iterates the left most index first/quickest in an array, then the right-most index last/slowest. You can use sub2ind() and ind2sub() to convert between a linear index and actual separated indexes.
Steven Lord
2020-8-11
You can work backwards.
First, which page contains 20? It can't be the first, because that only contains linear indices 1 through 12. We know this because the size of the array in the first two dimensions is [3 4]. So we know that element 20 is at subscripts (?, ?, 2).
Now we subtract off 12 to find the linear index of the corresponding element in the first page of a matrix. Now we're trying to find the row and column indices in a 3-by-4 matrix corresponding to linear index 8. It can't be in the first column (linear indices 1-3) or the second (4-6) so we know it's in the third column, at (?, 3, 2).
We subtract off 6 to find the linear index of the corresponding element in the first column. This tells us that a linear index of 20 in a 3-by-4-by-2 array corresponds to the element at subscripts (2, 3, 2).
I've done this handwavingly, but you can formalize it using remainders (rem) as ind2sub does.
Venkata Khambhammettu
2020-8-11
编辑:Venkata Khambhammettu
2020-8-12
Sudheer Bhimireddy
2020-8-11
A = rand(10,10,10);
B = A(1:2,1:2,1:2); %<- indexing the first two values in all dimensions so it creates a 2x2x2 matrix
C = A(1,1,1); %<- indexing the first value in all dimensions so it points to a single value
4 个评论
Stephen23
2020-8-11
Note that this answer uses subscript indexing only, no linear indexing is shown.
Venkata Khambhammettu
2020-8-11
Image Analyst
2020-8-11
Not sure how you deduced that. Of course it does work. You just want to know "how MatLab able to use linear indexing to calculate the location of an element in a 3D matrix." and it's in column major order, basically by iterating the left most index first, then the second index, then finally the third index the slowest.
"So the concept of Linear Indexing doesn't work for 3D matrices ?"
Of course linear indexing works with 3D arrays, just as the documentation that I linked to clearly states: "Another method for accessing elements of an array is to use only a single index, regardless of the size or dimensions of the array. This method is known as linear indexing" (bold added).
This answer does not show any linear indexing though, so it is unrelated to your question.
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!