Create a 4D array from 3D array with 3 columns, and 1D (row only) array with 3 columns

9 ビュー (過去 30 日間)
Jartok
Jartok 2024 年 1 月 15 日
コメント済み: Jartok 2024 年 1 月 18 日
When I load a test data file, Matlab tells me it has 4 dimensions, and when I check size, I see:
>> size(myVar)
ans =
3 241 161 161
'3' because I think the variable contains a 3D array.
'241 161 161' because the variable also contain a 1D array that has one row with three columns.
(Or '241 161 161' could mean there are three, 1D arrays with one column in each? I don't know how to check.)
Either way, I know that 241, 161, 161 is the dimension of a space in a 3D grid-like structure.
I want to create a .mat file with similar structure from the following two arrays:
1st array:
I have a 3D array, with size {576 3 4}, it looks something like,
(:,:,1) =
-0.0336 0.6440 -0.1464
-0.0336 0.6441 -0.1467
...... ....... .......
576 rows.
Also similar structure for (:,:,2), (:,:,3) and (:,:,4).
2nd array:
1D, row-only array with three columns
L = [ 100 100 100];
How do I create a 4D variable that has data from arr1 and arr2, and looks like what I wrote in the beginning?
My guess is its size would look like '3 100 100 100'.
The .mat creating part is the easy part:
save('myFile.mat','4D_var');
But I don't know how to create that 4D_var.
Thank you.
  4 件のコメント
Matt J
Matt J 2024 年 1 月 15 日
編集済み: Matt J 2024 年 1 月 15 日
Nx = length(myVar(1,:,1,1)); gives 241 ... So, could it be that myVar has 4 arrays inside it?
No.
What would you say about myVar(2,:,3,5)? You will see that length(myVar(2,:,3,5)) is also 241. Wouldn't that imply, by your reasoning, that myVar also contains a 5th array which is not in your list above and which also has 241 "rows"?
More generally, you will see that any myVar(i,:,j,k) gives a different length-241 vector for any combination of (i,j,k). There are many more than 4 such combinations.
Jartok
Jartok 2024 年 1 月 15 日
Ok, I got your point about the "more generally,... " part.
Thanks.
Can you please give me some hints on how would I go about creating a 4D variable with the inputs mentioned above? (After "my inputs are 3D array of size..."

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

採用された回答

Matt J
Matt J 2024 年 1 月 15 日
編集済み: Matt J 2024 年 1 月 15 日
How do I create a 4D variable that has data from arr1 and arr2, and looks like what I wrote in the beginning? My guess is its size would look like '3 100 100 100'.
I don't see what arr1 has to do with anything, but the following might be the "grid space" you're trying to construct,
L=[100,100,100];
[X,Y,Z]=ndgrid( 1:L(1) , 1:L(2) , 1:L(3) );
grid_space=permute(cat(4, X,Y,Z) ,[4,1,2,3] );
size(grid_space)
ans = 1×4
3 100 100 100
  24 件のコメント
Jartok
Jartok 2024 年 1 月 17 日
@Matt J Thank you!
Let me report back how it goes with my data.
Jartok
Jartok 2024 年 1 月 18 日
That was it! It works for me.
Thank you very much!!!

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

その他の回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2024 年 1 月 15 日
If understood correctly, this is what you are trying to get, e.g.:
rng(13) % for reproducibility
A = rand(5, 3);
size(A)
ans = 1×2
5 3
% B and C have the same size as A
B = rand(5, 3);
C = rand(5, 3);
L(:,:, 1) = A;
L(:,:, 2) = B;
L(:,:, 3) = C;
size(L)
ans = 1×3
5 3 3
% D array
D = rand(5, 3)
D = 5×3
0.2530 0.6861 0.1518 0.3793 0.5483 0.9260 0.6045 0.1380 0.6801 0.7724 0.0988 0.2377 0.0679 0.2456 0.5689
L(:,:,4) = D;
% Final 4-D array
[Row, Col, Layer]=size(L)
Row = 5
Col = 3
Layer = 4
L
L =
L(:,:,1) = 0.7777 0.4534 0.0350 0.2375 0.6090 0.2984 0.8243 0.7755 0.0585 0.9657 0.6416 0.8571 0.9726 0.7220 0.3729 L(:,:,2) = 0.6798 0.9491 0.0651 0.2563 0.2179 0.6298 0.3476 0.3194 0.8738 0.0094 0.9178 0.0087 0.3583 0.0319 0.7466 L(:,:,3) = 0.8128 0.9556 0.2770 0.0757 0.0000 0.6954 0.6565 0.2470 0.9186 0.5093 0.7122 0.2445 0.4799 0.3246 0.4581 L(:,:,4) = 0.2530 0.6861 0.1518 0.3793 0.5483 0.9260 0.6045 0.1380 0.6801 0.7724 0.0988 0.2377 0.0679 0.2456 0.5689
  1 件のコメント
Jartok
Jartok 2024 年 1 月 15 日
Thank you, SE, for creating sample code.
Which one is the 4D array?
I did size(L), which gives me
ans =
5 3 4
Which is not a 4D array.

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


Catalytic
Catalytic 2024 年 1 月 17 日
編集済み: Catalytic 2024 年 1 月 17 日
So, could it be that myVar has 4 arrays inside it
To me, it doesn't sound like you are talking about a 4D array at all, but instead a cell array. A cell array is a variable that can have "4 arrays inside it", for example:
xyz=rand(576,3,4);
Bx=rand(100,100,100);
By=rand(100,100,100);
Bz=rand(100,100,100);
whos xyz Bx By Bz
Name Size Bytes Class Attributes Bx 100x100x100 8000000 double By 100x100x100 8000000 double Bz 100x100x100 8000000 double xyz 576x3x4 55296 double
myVar={ xyz, Bx,By,Bz}
myVar = 1×4 cell array
{576×3×4 double} {100×100×100 double} {100×100×100 double} {100×100×100 double}
Here the first array is a 3D array of dimension 576x3x4 and the second,third, and fourth arrays are each 100x100x100
size(myVar{1})
ans = 1×3
576 3 4
size(myVar{2})
ans = 1×3
100 100 100
size(myVar{3})
ans = 1×3
100 100 100
size(myVar{4})
ans = 1×3
100 100 100
  2 件のコメント
Jartok
Jartok 2024 年 1 月 17 日
Thank you for your comments.
But no, no the 'xyz' variable (or 'myVar' that I mentioned at the beginning) is indeed a 4D variable.
Jartok
Jartok 2024 年 1 月 17 日
You asked: Do they form an equi-spaced lattice of points?
Maybe you already read my comments, but in that volume of space (in the test data case, a volume of 240x160x160, in my own case, I wanted it to be 100x100x100, to begin with, will adjust later if necessary), the program will create little cubic grids (with each grid size of 0.25).
My understanding is that each node of a cube has a "node index", and some associated (x,y,z) coordinate, and corresponding magnetic field values (Bx,By,Bz).
I am making it up as I am typing- that maxium value of node index in x,y and z-direction are 241, 161, 161.
I am probably mixing terminology, but that's what I meant by volume dimension, or total grid dimension in my earlier comments.

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

カテゴリ

Help Center および File ExchangeInputs についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by