How to select column form 3D array and use it in other 3D array?

22 visualizaciones (últimos 30 días)
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
  3 comentarios
Mantas Vaitonis
Mantas Vaitonis el 19 de Jun. de 2018
As mentioned a is 3D array (5x1x499) where colum stores valuse 1:5 in random, thus b(500,a(:,1,:),:) should return 3D array with indexed valuse by a, like in 2D array would be x(:,y(:,1)), where x is (500x5) and y is (5X1).
Mantas Vaitonis
Mantas Vaitonis el 19 de Jun. de 2018
Now if I do b(500,a(:,1,:),:), the array is of size (1x2495x499), but it should be (1x5x499).

Iniciar sesión para comentar.

Respuesta aceptada

Zhangxi Feng
Zhangxi Feng el 19 de Jun. de 2018
Editada: Zhangxi Feng el 19 de Jun. de 2018
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 comentarios
Mantas Vaitonis
Mantas Vaitonis el 19 de Jun. de 2018
Yes I did want to rearrange the 500x[1 2 3 4 5]x499 matrix into 500x[2 1 3 4 5]x499. I was looking for way without the loop, then you confirm that there is no other way, only to use for loop. As you seem really good on 3D arrays I am looking as well a way to use find on 3D array, that it would store in roes indexes of this function idx=find(abs(L(:,:,:)>t)), however it returns one column with indexes form 1:n. With 2D it would look like [idx]=find(abs(L(:,:))>t)
Zhangxi Feng
Zhangxi Feng el 19 de Jun. de 2018
Editada: Zhangxi Feng el 19 de Jun. de 2018
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by