How to select column form 3D array and use it in other 3D array?
25 次查看(过去 30 天)
显示 更早的评论
Hello,
I have two arrays a(5x1x499) and b(500x5x499), would like to select column form 3D array of a and use it as indexing on array b in fashion like b(500,a(:,1,:),:). What would be the option without using for loops. Data stored is gpuArray
采纳的回答
Zhangxi Feng
2018-6-19
编辑:Zhangxi Feng
2018-6-19
In your case, a(:,:,n) would actually return the column vector you desire, where n is the nth column since your a matrix is 499 single columns.
I assume you want to use the 499 5x1 arrays as index to rearrange the 500x5x499 matrix. I.e. a(:,:,1) = [2 1 3 4 5] would rearrange the 500x[1 2 3 4 5]x499 matrix into 500x[2 1 3 4 5]x499 correct?
Then you simply needs to do: b(:,a(:,:,n),:).
However, if you want to go through ALL of a, you must understand each a(:,:,n) returns a rearranged 500x5x499 matrix, that means you are looking for 499 matrices of the size 500x5x499. There is no better way to get them all besides using a loop, and storing each of the 499 rearranged matrix separately.
If you really want to keep them all in one huge matrix and do this in one go, I believe c = b(:,a,:) would do the job. I verified that it returns a 500x2495x499 matrix, which implies that it contains all 499 rearranged 500x5x499 matrices, but I have not yet confirmed if the indexing is each 500x5x499 next to each other or not.
2 个评论
Zhangxi Feng
2018-6-19
编辑:Zhangxi Feng
2018-6-19
Some functions in MATLAB are not meant to be used beyond 2D arrays. For example, the function size() cannot be used to show the sizes of three cells with different size strings. It is a simple edition but it is simply not implemented as such. It is the same reason that find for 3D array returns one column instead of one number. But it should still return the row and col if you use [row,col] = find, and it makes sense at a row,col you should expect a column of output for a 3D array.
However, since your data is not unique and is repeated rows and columns, in a common sense, I am not sure if using find will yield you the desired outputs. Maybe you can use a second find after one find to locate the exact z-index in the result column at the returned [row,col], but it will still give you all the values that satisfy the find condition, which will span throughout the entire matrix.
I mentioned that you can use c = b(:,a,:) and I used a smaller matrix to confirm that it does store the rearranged matrices in order. But I will leave it to you to figure out the exact indexing of the desired set of arranged 500x5x499 matrix.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!