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 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 29 日
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.
ML
ML 2014 年 12 月 30 日
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!

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2014 年 12 月 30 日

1 投票

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,[]);

1 件のコメント

ML
ML 2014 年 12 月 30 日
Thank you, that works perfectly.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

タグ

質問済み:

ML
2014 年 12 月 29 日

コメント済み:

ML
2014 年 12 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by