convert data from two 3D arrays into three 2D arrays

조회 수: 1(최근 30일)
Pawel Labedzki
Pawel Labedzki 2021년 6월 13일
댓글: Pawel Labedzki 2021년 6월 13일
I have two 3D arrays A and B with dimensions n1 x n2 x n3. Indices i = 1:n1, j = 1:n2, k:1:n3 represent values of some variables var1, var2, var3 (for example var1(i) = var1Min + (i-1)*delta_var1).
I would like to convert two given 3d arrays A and B into three 2d arrays var1Array, var2Array, var3Array, with values of the variables var1, var2, var3 and indices representing values in arrays A and B (for example var1Array(i,j), i represents data from A, j represents data from B, i represents Adata = minA + (i-1)*delta_A, j represents data from B, Bdata = minB + (j-1)*delta_B)
Are there any functions to do it automatically in MATLAB? If not, please, give me some hints how to do it.

답변(2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 6월 13일
편집: Sulaymon Eshkabilov 2021년 6월 13일
It is quite simple:
var1Array = A(:,:,1); var2Array = A(:,:,2); var3Array=A(:,:,3);
% OR combining A and B into one array
var1Array = [A(:,:,1);B(:,:,1)] var2Array = [A(:,:,2); B(:,:,2)];var3Array=[A(:,:,3);B(:,:,3)]
...
  댓글 수: 1
Pawel Labedzki
Pawel Labedzki 2021년 6월 13일
I would like to do something more sophisticated. I explain it in following example:
A = zeros(2,2,2);
A(:,:,1) = [1.2 1.3; 1.4 1.7];
A(:,:,2) = [1.8 1; 2.1 2.3];
B = zeros(2,2,2);
B(:,:,1) = [1 2; 4 3];
B(:,:,2) = [5 7; 10 21];
sort(reshape(A,[1,8])) %values of A
sort(reshape(B,[1,8])) %values of B
A and B values:
ans =
1.0000 1.2000 1.3000 1.4000 1.7000 1.8000 2.1000 2.3000
ans =
1 2 3 4 5 7 10 21
Now let:
var1 = [3 2];
var2 = [-1 3];
var3 = [5,6];
%then: var1Matrix, var2Matrix, var3Matrix - 8x8 matrices containing values from above arrays
%indices of these matrices represent values 1, 1.2, 1.3, 1.7, 1.8, 2.1, 2.3
%for A and 1, 2, 3, 4, 5, 7, 10, 21 for B
I don't know how to explain it better, sorry :)

댓글을 달려면 로그인하십시오.


madhan ravi
madhan ravi 2021년 6월 13일
A = rand(2,2,2)
A =
A(:,:,1) = 0.6237 0.5925 0.7010 0.7094 A(:,:,2) = 0.9245 0.4186 0.5723 0.0444
B = rand(2,2,2)
B =
B(:,:,1) = 0.3876 0.4998 0.9857 0.8470 B(:,:,2) = 0.0038 0.3857 0.0710 0.3389
Data = [A; B] % naming variables is a bad idea , keep them concatenated as one 3D matrix, is much simpler than naming each page
Data =
Data(:,:,1) = 0.6237 0.5925 0.7010 0.7094 0.3876 0.4998 0.9857 0.8470 Data(:,:,2) = 0.9245 0.4186 0.5723 0.0444 0.0038 0.3857 0.0710 0.3389

Community Treasure Hunt

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

Start Hunting!

Translated by