Create a 4D array from 3D data set and a 2D variable location index

Hello, I am having some problems getting my head around creating a 4D array, and would appreciate some help with the fine tuning!
I have a set of 36 data sets, each of which is a 250 x 2 array. These comprise 12 readings, each repeated twice (so there are 3 sets of each reading which I will use to calculate a mean). These data sets are stored in data which is a 250 x 2 x 36 variable. I also have a matrix A which gives the location index of each 250 x 2 data set when I read them all in using a combination of files=dir() and dlmread(files.name). A looks like this:
A = [2 8 3 9 4 10 5 11 6 12 7 1;
14 20 15 21 16 22 17 23 18 24 19 13 ;
26 32 27 33 28 34 29 35 30 36 31 25];
I would like to create a 4D matrix out of my 2D data sets by having a 3rd dimension which is the 12 different readings, and a 4th dimension which is the 3 incidences of each set.
So far I have used the cat() function in this way:
set= cat(4, data(:,:,A(1,:)), data(:,:,A(2,:)), data(:,:,A(3,:)));
I am sure though that there must be a more elegant way to do this without manually typing out 'data(:,:,A(1,:))' etc as this would not work if I had many more repeats.
Is there a better way for me to do this? I thought set= cat(4, data(:,:,A)) might work, but it takes A as a 1D list rather than a 2D matrix.
Thank you for your time!

2 Comments

I think you can make your question more clear if you post a simple and short example, and avoid describing things that will not help.
Ok thanks for your feedback, here's another go at posing the question:
I have a 3D array a x b x c (called readings). c is a linear index of data a x b. I would like to change the linear index to a 2D array index d x e (called A) to group the data in a more meaningful way, thereby creating a 4D array. I can do this using
cat(4, readings(:,:,A(1,:)), readings(:,:,A(2,:)), readings(:,:,A(3,:)))
but I would like to know if there is a better way of doing this without specifying each of the rows of A individually or using a for loop.
Thanks!

Sign in to comment.

 Accepted Answer

Looks like you could just do
[m,n]=size(A);
[p,q]=size(readings);
A=A.';
set=readings(:,:,A(:));
set=reshape(set,p,q,n,[]);

More Answers (0)

Categories

Tags

Asked:

ML
on 29 Dec 2014

Commented:

ML
on 30 Dec 2014

Community Treasure Hunt

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

Start Hunting!